将逗号分隔的JSON转换为换行符分隔的节点

时间:2015-09-23 22:51:11

标签: json node.js jq fs jsonlines

我有一个JSON文件,我正在阅读节点,修改并将其保存为json文件。

我希望将新json保存为换行符分隔而不是数组。

我遇到了https://github.com/CrowdProcess/newline-json,但并不完全了解溪流。如果我有以下流设置,我如何通过解析器和字符串管道它?

ga

但是运行以下内容只会输出一个空白文件。

fileStream = fs.createReadStream('source.json')
writeStream = fs.createWriteStream('output.txt');

var Parser = require('newline-json').Parser;
var Stringifier = require('newline-json').Stringifier;

var parser = new Parser();
var stringifier = new Stringifier();

我对溪流的看法是什么?

3 个答案:

答案 0 :(得分:6)

将JSON数组转换为换行符分隔的JSON实体流的一种方法是使用带有-c选项的jq,例如

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


// Creating structure for node
struct test_struct
{
int val;         // val is member id number
char name;
char lastn;
int age;
struct test_struct *next;
};




// declaring global head and curr pointers
struct test_struct *head = NULL;
struct test_struct *curr = NULL;






// creating a list
struct test_struct* create_list(int val, char* name, char* lastn, int age)
{
printf("\n creating list with head node as [%d] [%s] [%s] [%d] \n", val, name, lastn, age);

struct test_struct *ptr = malloc(sizeof(struct test_struct)); // creating list
if(NULL == ptr)
{
  printf("\n Node creation failed \n");
  return NULL;
}

ptr->val = val;
ptr->name = *name;
ptr->lastn = *lastn;
ptr->age = age;
ptr->next = NULL;

head = curr = ptr;

return ptr;

}





// add member to list
struct test_struct* add_to_list(int val, char *name, char *lastn, int age, bool add_to_end)
{
if(NULL == head)
{
    return (create_list(val, name, lastn, age));
}


if(add_to_end)
{
printf("\n Adding node to end of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
}
else
{
printf("\n Adding node to beginning of list with data [%d] [%s] [%s] [%d] \n", val, name, lastn, age);
}

struct test_struct *ptr = malloc(sizeof(struct test_struct));

if(NULL == ptr)
{
    printf("\n Node creation failed \n");
    return NULL;
}

ptr->val = val;
ptr->name = *name;
ptr->lastn = *lastn;
ptr-> age = age;
ptr-> next = NULL;

if (add_to_end)
{
    curr-> next = ptr;
    curr = ptr;
}
else
{
    ptr -> next = head;
    head = ptr;
}

return ptr;
} 






//search a name in created list
struct test_struct* search_in_list(char name, char lastn, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;

printf("\n Searching the list for the value [%s][%s]\n", name, lastn);

while(ptr != NULL)  // searching loop
{
    if(ptr->name == name && ptr->lastn == lastn)
    {
        found = true;
        break;
    }
    else
    {
        tmp = ptr;
        ptr = ptr->next;
    }
}
return ptr;

if(true == found)
{
    if(prev)
    {
    *prev = tmp;
    return ptr;
    }

    else
    {
    return NULL;
    }

  }
 }






//printing the list
void print_list(void)
{
struct test_struct *ptr = head;

printf("\n -----Printing list Start----- \n");

while(ptr != NULL)
{
    printf("\n [%d] \n", ptr -> val);
    printf("\n [%s] \n", ptr -> name);
    printf("\n [%s] \n", ptr -> lastn);
    printf("\n [%d] \n", ptr -> age);
    ptr = ptr->next;
}

printf("\n -----Printing list end---- \n");

return;
}




//printing the list 2 is for printing age only
void print_list2(void)
{
struct test_struct *ptr = head;

printf("\n -----Printing list Start----- \n");

while(ptr != NULL)
{
    printf("\n [%d] \n", ptr -> age);
    ptr = ptr->next;
}

printf("\n -----Printing list end---- \n");

return;
}




// main function
int main(void)
{
char n, l;
struct test_struct *ptr = NULL;


// for adding member to list
    add_to_list(123, "william", "shakespeare", 30, true);
    add_to_list(124, "william", "gibson", 35, true);
    add_to_list(125, "chuck", "palahniuk", 40, true);
    add_to_list(126, "mario", "puzio", 50, true);
    add_to_list(127, "umberto", "eco", 60, true);
    add_to_list(128, "ezra", "pound", 125, true);

    print_list();




// for searching name in list
    ptr = search_in_list(n, l,  NULL);
    if(NULL == ptr)
    {
        printf("\n Search [name = %s] [lastn = %s] failed, no such element found \n", n, l);
    }
    else
    {
        printf("\n Search passed [name = %s] [lastn = %s] \n", ptr->name, ptr->lastn);
    }


    print_list();



return 0;
}

输入:

$ jq -c ".[]"

输出:

[[1,2], 3, {"4":5}]

请参阅https://stedolan.github.io/jq

答案 1 :(得分:0)

在node.js中,您可以使用node-jq包来完成@peak上面显示的操作。

var stream = require('stream');
var fs = require('fs');
const jq = require('node-jq');

var fileName = 'YOUR_FILE_NAME'; //e.g abc.json
var bucketName = 'YOUR_BUCKET NAME'; // e.g gs://def
var dataStream = new stream.PassThrough();


async function formatJson() {

    jq.run('.[]', fileName, {output: 'compact'})
    .then((output) => {
        dataStream.push(output)
        dataStream.push(null)
        console.log(dataStream)    
    })
    .catch((err) => {
        console.log(err)
    })
}

formatJson()

我不是经验丰富的节点人员,所以如果代码笨拙但可以使用,请您道歉。

答案 2 :(得分:0)

对于正在寻求如何将对象的json数组转换为nd-json的解决方案的任何人。解决方法如下:

输入:

const arrObj = [{
  id: 1,
  name: 'joe'
}, {
  id: 2,
  name: 'ben'
}, {
  id: 3,
  name: 'jake'
}, {
  id: 4,
  name: 'marsh'
}];

// stringify the object and join with \n
const ndJson = arrObj.map(JSON.stringify).join('\n');

console.log(ndJson);

输出:

{"id":1,"name":"joe"}
{"id":2,"name":"ben"}
{"id":3,"name":"jake"}
{"id":4,"name":"marsh"}

示例用例:从json文件将批量请求导入到 elasticsearch 时。

快乐编码:)