0

I need to extract the text value of productid from order but unfortunately i haven't been able to traverse the JSON. Any ideas about how to traverse the nodes in Node JS the simplest way possible ?

{
  "order": {
    "PRD_SHIRT_048": {
      "price": "40.99",
      "productId": "PRD_SHIRT_048",
      "quantity": "1"
    },
    "PRD_TOP_047": {
      "price": "40.99",
      "productId": "PRD_TOP_047",
      "quantity": "1"
    }
  }
}
homersimpson
  • 4,124
  • 4
  • 29
  • 39
Anu
  • 51
  • 7
  • 1
    Possible duplicate of [How to parse JSON using Node.js?](https://stackoverflow.com/questions/5726729/how-to-parse-json-using-node-js) – vahdet Apr 04 '19 at 06:17
  • 1
    Can you be more specific about what the exact issue is and what you've tried? Do you want to extract an array of all the `productId`s for each object in `order`? Is the provided json retrieved from an API and you're stuck in figuring out how to handle the API response? What is the desired output? – homersimpson Jan 16 '21 at 08:56

3 Answers3

1

First of all, you need to decide where you want to extract the data from. If it is from a file you need to import the file with for example the npm package 'fs'.

Example code:

const fs = require("fs");
const content = fs.readFileSync("content.json");

console.log("Output: \n" + content);

You can use axios to get the json data from a specific url. For example:

axios.get('yoururl')
  .then((response) => {
    // handle success
    console.log(response);
  })
  .catch((error) => {
    // handle error
    console.log(error);
  });

Then you have extracted the data successfully. After that you can parse the json content with JSON.parse and that will return an object with all its content.

For example for your code:

const json = `{"order": {
    "PRD_SHIRT_048": {
      "price": "40.99",
      "productId": "PRD_SHIRT_048",
      "quantity": "1"
    },
 "PRD_TOP_047": {
      "price": "40.99",
      "productId": "PRD_TOP_047",
      "quantity": "1"
    }
  }}`;
  
const obj = JSON.parse(json);

console.log(obj.order.PRD_SHIRT_048.productId);

If you want, you can iterate over the objects from the order object and get the product id from that.

FloWy
  • 934
  • 6
  • 14
  • Here PRD_SHIRT_048 is a product id how can i extract that value from the json console.log(obj.order.PRD_SHIRT_048.productId); – Anu Apr 04 '19 at 06:54
  • Well you bassically did with obj.order.PRD_SHIRT_048.productId But if you can also do obj.order.PRD_SHIRT_048. Or you simply loop over the objects from the object order. – FloWy Apr 04 '19 at 06:57
1

If I understand your question correctly, you are looking to extract the productIds?

Here is a solution using vanilla javascript

const data = { order: { PRD_SHIRT_048: { price: '40.99', productId: 'PRD_SHIRT_048', quantity: '1' }, PRD_TOP_047: { price: '40.99', productId: 'PRD_TOP_047', quantity: '1' } } };

console.log(Object.keys(data.order));
// => [ 'PRD_SHIRT_048', 'PRD_TOP_047' ]
console.log(Object.values(data.order).map(({ productId }) => productId));
// => [ 'PRD_SHIRT_048', 'PRD_TOP_047' ]
.as-console-wrapper {max-height: 100% !important; top: 0}

Or if you need a more flexible solution (i.e. multiple different paths for the productId, nested productIds etc), you could consider using a library

// const objectScan = require('object-scan');

const data = { order: { PRD_SHIRT_048: { price: '40.99', productId: 'PRD_SHIRT_048', quantity: '1' }, PRD_TOP_047: { price: '40.99', productId: 'PRD_TOP_047', quantity: '1' } } };

console.log(objectScan(['order.*.productId'], { rtn: 'value' })(data));
// => [ 'PRD_TOP_047', 'PRD_SHIRT_048' ]
console.log(objectScan(['order.*'], { rtn: 'property' })(data));
// => [ 'PRD_TOP_047', 'PRD_SHIRT_048' ]
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>

Disclaimer: I'm the author of object-scan

vincent
  • 1,953
  • 3
  • 18
  • 24
-1

const json = `{"order": {
    "PRD_SHIRT_048": [
      "price",
      "productId",
      "quantity"
    ],
 "PRD_TOP_047": [
      "price",
      "productId",
      "quantity"
    ]
  }}`;
  
const obj = JSON.parse(json);

console.log(obj.order.PRD_SHIRT_048.productId);
Chethan
  • 3
  • 1