Jin Park
Jin Park

Reputation: 461

How to split String from json format in Node js

I'm newbie at Node.js, here is my sample json format data. I want to split them into json format and need to get tid values. In node.js I tried to use JSON.parse(data) but it occured an error (becasue of [] symbols). is there a simple way to split them into json format and get the tid values?

[{"date":1535624757,"date_ms":1535624757427,"amount":90.389717,"price":0.00860497,"type":"buy","tid":251916659},{"date":1535624757,"date_ms":1535624757428,"amount":99.428689,"price":0.00860497,"type":"buy","tid":251916662},{"date":1535624757,"date_ms":1535624757469,"amount":384.65968,"price":0.00860497,"type":"buy","tid":251916663}]

Upvotes: 1

Views: 3866

Answers (4)

Fupeng Wang
Fupeng Wang

Reputation: 94

JSON.parse takes a string variable,

[
    {
        "date": 1535624757,
        "date_ms": 1535624757427,
        "amount": 90.389717,
        "price": 0.00860497,
        "type": "buy",
        "tid": 251916659
    },
    {
        "date": 1535624757,
        "date_ms": 1535624757428,
        "amount": 99.428689,
        "price": 0.00860497,
        "type": "buy",
        "tid": 251916662
    },
    {
        "date": 1535624757,
        "date_ms": 1535624757469,
        "amount": 384.65968,
        "price": 0.00860497,
        "type": "buy",
        "tid": 251916663
    }
]

this code you goven is object variable.

you can use this

var data = JSON.parse('[{"date":1535624757,"date_ms":1535624757427,"amount":90.389717,"price":0.00860497,"type":"buy","tid":251916659},{"date":1535624757,"date_ms":1535624757428,"amount":99.428689,"price":0.00860497,"type":"buy","tid":251916662},{"date":1535624757,"date_ms":1535624757469,"amount":384.65968,"price":0.00860497,"type":"buy","tid":251916663}]')
console.log(data)

Upvotes: 2

Brian Hart
Brian Hart

Reputation: 74

It appears as though you're trying to work with a Javascript object instead of a string that contains JSON encoded data. Consider working with the object directly. For example, you could use a for loop to iterate over the elements of data as follows:

const data =  [ {"date":1535624757,"date_ms":1535624757427,"amount":90.389717,"price":0.00860497,"type":"buy","tid":251916659},{"date":1535624757,"date_ms":1535624757428,"amount":99.428689,"price":0.00860497,"type":"buy","tid":251916662},{"date":1535624757,"date_ms":1535624757469,"amount":384.65968,"price":0.00860497,"type":"buy","tid":251916663}];

for (var i = 0; i < data.length; i++) {
  console.log(data[i].tid);
}

Upvotes: 2

Andy Gaskell
Andy Gaskell

Reputation: 31761

Since the array you posted is not JSON within the context of a JavaScript program (though it is a valid standalone JSON document), let's say that data is the value of your array:

const data = [{
  "date":1535624757,
  "date_ms":1535624757427,
  "amount":90.389717,
  "price":0.00860497,
  "type":"buy",
  "tid":251916659
}, {
  "date":1535624757,
  "date_ms":1535624757428,
  "amount":99.428689,
  "price":0.00860497,
  "type":"buy",
  "tid":251916662
}, { 
  "date":1535624757,
  "date_ms":1535624757469,
  "amount":384.65968,
  "price":0.00860497,
  "type":"buy",
  "tid":251916663
}];

You can pull out the tids like this:

const tids = data.map(e => e.tid);

tids:

[ 251916659, 251916662, 251916663 ]

If you would like tids as a JSON string, you can use JSON.stringify:

const jsonTids = JSON.stringify(tids);

jsonTids:

'[251916659,251916662,251916663]'

Upvotes: 0

Max Baldwin
Max Baldwin

Reputation: 3472

Because your JSON object is inside of an array (the [] symbol), you have to reference the JSON object by its index in the array. Like so.

const data = [{"date":1535624757,"date_ms":1535624757427,"amount":90.389717,"price":0.00860497,"type":"buy","tid":251916659},{"date":1535624757,"date_ms":1535624757428,"amount":99.428689,"price":0.00860497,"type":"buy","tid":251916662},{"date":1535624757,"date_ms":1535624757469,"amount":384.65968,"price":0.00860497,"type":"buy","tid":251916663}];

console.log(data[0]);

Here's some info on array's an their indexes.

Upvotes: -1

Related Questions