1

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}]
1
  • JSON.parse works perfectly fine for me, using that data Commented Aug 31, 2018 at 2:06

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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);
}

Comments

0

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]'

Comments

-1

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.

2 Comments

Your code throws. Again, the problem is almost certainly that JSON.parse takes a string, you can't feed it a JavaScript data structure. Please run your code before posting it as an answer.
@JaredSmith right, you don't event have to parse it. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.