Accessing data in nested json

hey everyone,
please excuse my beginners issues but I am having some really hard time accessing
items in nested json. At the moment I am trying to simply get the

  • title
  • url (found in source)
  • main_name (found in actors)
  • aliases (also found in actors)

from json that looks like this

 {
  "id": "123456789",
  "title": "brad pitt on the rise",
  "authors": "brian j. robb",
  "language": "EN",
  "sources": [{
    "id": "123456789",
    "name": "imdb",
    "url": "https://www.imdb.com/name/nm0000093/",
    "description": "brad pitt reference",
    "reports": null
  }],
  "references": [
    "https://www.imdb.com/name/nm0000093/"
  ],
  "report_names": [
    "brad pitt on the rise"
  ],
  "actors": [{
    "id": "123456789",
    "main_name": "brad pitt",
    "aliases": ["tyler durden", "Gummyhos"],
    "source_name": "tv:shows",
    "books": [
      "The Rise to Stardom",
      "The legendary Brad Pitt"
    ],
    "source_id": "amazon",
    "reports": null
  }]
}

I tried the following in a transformer without much luck

const data = {{query.data.data}}
let final_data = [];
Object.keys(data).forEach(key => {
  let new_obj = {}
  new_obj.reports = data[key]
  final_data.push(new_obj)
})
return final_data

any help is much appreciated!

Thank you!

Hey @bud_bundy! Just to clarify, is this the final JSON you're using or will you have a list of JSON objects that look like this?

If the first option (just this one JSON object at a time), we could just do:

return {title: data.title, url: data.sources[0].url, main_name: data.actors[0].main_name, aliases: data.actors[0].aliases}

Let me know if this works for you!

I have the exact same question. Except mine is in a list format. How would you do this when there is a repeated list of values? What if you have a case where the values which you want to pull are not all at the same level/under the same parent value?

what does the [0] mean at the end of the parent values?

Hey @bigshooTer! Are you using a list view? If so, these docs might be more helpful that I can be :sweat_smile:

https://docs.retool.com/docs/create-custom-list-views

As a quick note, you'll likely be using [i] to "index into" or select the desired row in your list.

You access an array element by referring to the index number.

[0] is the Javascript notation used to access or "index into" the first element of an array (array or list of components in this case). The count starts at 0 for arrays in Javascript.

https://www.w3schools.com/js/js_arrays.asp

Let us know if you have any questions at all! If this post auto-closes, feel free to write a new post anytime :slight_smile: