0

I want to assign the data inside a ndjson file into a js variable. I have tried putting it in an array and an object but that is throwing me error.

I have tried like this ...

var data = [{"attributes":{}}
{"attributes":{}}
{"attributes":{}}]

and

var data = {{"attributes":{}}
{"attributes":{}}
{"attributes":{}}}

But this is not working.

Can someone help me on how to assign this ndjson value to a js variable without throwing error.

MaKr
  • 13
  • 5
  • [ndjson](http://ndjson.org/) is useful for _streaming_ values (I had to look this up, having never heard of ndjson) - it is essentially an array of objects (and arrays?) but (1) the outermost brackets `[]` are omitted and (2) the separator between records is a newline instead of a comma. So… how are you getting/receiving this stream? You can `JSON.parse()` each item in the stream and _push_ the resulting object on an array, but how you parse depends on how you've received it, e.g. do you just have a big string full of newline delimited objects? – Stephen P May 12 '20 at 16:10

2 Answers2

1

ndjson is useful for streaming values — the format is essentially an array of objects, but (1) the outermost brackets [] are omitted so the array is implied, and (2) the separator between records is a newline instead of a comma. Basically a stream of lines where each line is a record in JSON format. The spec is not clear if a record/line can itself be an array, but objects can contain arrays.

Using the example presented in the spec, you must have received this stream of text in some way:

{"some":"thing"}
{"foo":17,"bar":false,"quux":true}
{"may":{"include":"nested","objects":["and","arrays"]}}

Let's say you've received this and stored it in a variable, which would be a string, input. You could then break this string on newlines with input.split('\n')
parse each one via JSON.parse(…) and save the result in an array.

let input = '{"some":"thing"}\n{"foo":17,"bar":false,"quux":true}\n{"may":{"include":"nested","objects":["and","arrays"]}}';
let result = input.split('\n').map(s => JSON.parse(s));

console.log('The resulting array of items:');
console.log(result);

console.log('Each item at a time:');
for (o of result) {
    console.log("item:", o);
}
Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • Upvoted for bunch of new information, thanks for the link of ndjson also – A l w a y s S u n n y May 12 '20 at 17:25
  • @Stephen the problem here is, i could not save store the ndjson value into a variable. It is throwing me error. This is what im trying to do, I have xyz.js -> i have created a variable in it var x= ndjson data, and i have assigned ndjson data to it. It is throwing me error. Im asking on how to assign ndjson value to it as mentioned. – MaKr May 13 '20 at 09:21
  • @MaKr - as I alluded to in my comment on your Q, a lot depends on how you are getting/receiving this ndjson, which you don't show in your question. Are you receiving it from doing a [fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API) or an AJAX request? Reading it from a file? Do you get it line-by-line? or all in one blob? If reading from a file, how? You are somehow getting a String that contains ndjson formatted text — show _**that**_ code. If you read one line from a file then that is just `o = JSON.parse(line)`. – Stephen P May 13 '20 at 16:21
0

Javascript array of the object will be like this, see the comma , between them,

var data = [{
  "attributes": {}
},{
  "attributes": {}
},{
  "attributes": {}
}];

console.log(data);

OR objects inside object can be like this and I guess you don't want this,

var data = {
  "attributes": {},
  "attributes": {},
  "attributes": {}
};

console.log(data);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103