0

First, thank you for any help you can provide! I've been away from coding for awhile and am having some trouble solving a problem I have with some dynamic JSON data and hoping someone can help me understand where I am stuck.

I am pulling some data in node using node-fetch and returning a JSON object with their data. Heres the structure that returns. JSON Object

I need to pull the assets: # based on its sibling template id and I cannot seem to figure it out. I can pull the data if I use data.data.templates[0].assets, but the template ID is not always going to be in the same[0] spot in the structure depending on how many templates the user has. I have checked a few posts here and thinking I may need to use JSON Expressions but I am not really getting how to make it work.

data.data.templates[templates == '210537'].assets); feels like I am close but no cigar yet.

Can anyone point me to a post that might help me understand what I am missing!? The extra nesting is throwing me off and I am so confused at this point!

I've looked over this post json sibling data and a few others around the web but still stuck. Also it's a Node project and I am fetching the data from the web using the node-fetch module. I don't think that matters but just a little more info incase it does.

StevenHolder
  • 95
  • 1
  • 2
  • 10

1 Answers1

1

If you want to use this kind of syntax, I suggest jsonpath

Example:

const jp = require("jsonpath");

const cities = [
    { name: "London", "population": 8615246 },
    { name: "Berlin", "population": 3517424 },
    { name: "Madrid", "population": 3165235 },
    { name: "Rome", "population": 2870528 }
];

const population = jp.query(cities, "$[?(@.name =='London')].population");
console.log(population);
//[ 8615246 ]
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20