简体   繁体   中英

Standardize keys in array of javascript objects

I am dealing with a JSON response from a webservice that drops keys if the value is null. This practice is reasonable enough, but I am using the the response as a data source for a reporting component that uses the first value in the array of objects as the source of truth for all fields available within the rest of the objects.

The reporting component is calling a serverless javascript endpoint that I write and control. So I can process the results and potentially standardize the JSON object keys in this js code prior to send the response.

Making things trickier, the shape and/or fields of the data is variable. It is the result of a user supplied GraphQL query.

So a query like this:

    query GetCustomersByState($state: String!) {
      Customer(where: { State: { _eq: $state } }) {
        CustomerID
        CustomerName
        Address1
        Address2
        City
        State
        Zip
      }
    }

Might result in:

[
    {
      CustomerID: 'TENASPH',
      CustomerName: 'Tennessee Asphalt',
      Address1: '123 main st',
      City: 'Somewhere',
      State: 'TN'
    },
    {
      CustomerID: '10002',
      CustomerName: 'A&N Drywall',
      Address1: '123 Drywall Ln',
      Address2: 'Suite 2',
      City: 'Somewhere',
      State: 'TN',
      Zip: '12345'
    },
    {
      CustomerID: 'JONES',
      CustomerName: 'Jones Bros Construction',
      City: 'Somewhere',
      State: 'TN',
      Zip: '37917'
    },
]

How can I standardize the keys across the array of objects?

(If helpful, the data source is a Hasura GraphQL server instance. I have looked around the Hasura interface and can't seem to find any settings that would include blanks in the response. Would be great if that did exist.)

You could set the keys ahead of time by finding all the unique ones and then validate for them in a map()

 let response = [{ CustomerID: 'TENASPH', CustomerName: 'Tennessee Asphalt', Address1: '123 main st', City: 'Somewhere', State: 'TN' }, { CustomerID: '10002', CustomerName: 'A&N Drywall', Address1: '123 Drywall Ln', Address2: 'Suite 2', City: 'Somewhere', State: 'TN', Zip: '12345' }, { CustomerID: 'JONES', CustomerName: 'Jones Bros Construction', City: 'Somewhere', State: 'TN', Zip: '37917' }, ] let keys = [...new Set(response.flatMap(Object.keys))]; //console.log(keys) let standardized = response.map(e => { keys.forEach(k => { if (!e[k]) e[k] = ''; }) return e }) console.log(standardized)

Your query itself that lists all fields should be the source of truth of all present fields, not the first returned object. What if there are no returned objects anyway?

GraphQL actually supports introspection , which should allow you to query the fields of an object. I'm not sure whether that allows you to query the fields of a query itself though, but again: the query should be written manually/once, therefore you should already know which fields exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM