Flatten/unnest JSON queries to a table. Help?

I have a GraphQL query JSON file that has nested components that I want to flatten. I need to do this for a number of queries before I join them and injest into my db. Anyone been successful with a simple js transformer that is replicable to multiple queries?

Hey @Bill_Waring, How you flatten your data will depend on how your data is structured. Could you share a screenshot or sample of your data so we can see its structure?

Hi there. I'm a GraphQL / js non-professional and have also struggled with this. I have found the following syntax incredibly useful as soon as the nesting hits an array of objects:

const x_data = rec_details_long.data.properties.data


const x_data_2 = x_data
	.map(
	({id,
    lastSalePropertyPrice:{value:lastSalePropertyPrice},
    listedPropertyOfferPrice:{value:listedPropertyOfferPrice},
    property:{price:{value:propertyprice}, finalListingPrice:{value:finalListingPrice}}
	
    }) =>
	({id,
	    lastSalePropertyPrice,
	    listedPropertyOfferPrice,
	    propertyprice, finalListingPrice
	})
)
	
return x_data_2

The original JSON looks like this (I have edited out a subsection, I may have got the closing brackets wrong but you'll need to edit that anyway):

{
  "data": {
    "properties": {
      "data": [
        {
          "id": "a22a2e0e-6bfd-4107-87dc-f010f94699ef",
          "lastSalePropertyPrice": {
            "value": 165000,
            "code": "USD"
          },
          "listedPropertyOfferPrice": {
            "value": 165000,
            "code": "USD"
          },
          "property": {
            "price": {
              "value": 165000,
              "code": "USD"
            },
            "finalListingPrice": {
              "value": 165000,
              "code": "USD"
            }
            }
        },
       ],
     },
   },
}

So you can see that what the js is doing is navigating down the tree, and when it reaches the specific item it then assigns an alias (e.g. it sets property.price.value to be called 'propertyprice' which is then passed to the second part of the map function. Some kind of object destructuring magic apparently.

hth
dominic

Thank you, @domjammoo I'm going to give this a try. My query is against Monday.com's board schema, which is pretty straightforward.

image

@everett_smith Here's an example of the existing output:

@ Bill_Waring

Hey there :wave:

Can I ask what in this output you would like to flatten?