EDIT: Whoops, forgot the config

Try to write the most efficient and concise code

Sure... for me "efficient" means "understandable" - if they wanted performance they should have said "performant" isntead :). And concise code... well, that doesn't always mean quality code, so I'll try to balance.

My approach would be to at least somewhat unify the data, then use smaller functions and deal with the super confusing data structure there.

If this was a very big project and a more generic project would actually be needed, only then would I consider making anything generic with such data. Often it's not actually necessary and just introduces a whole lot of complexity.

const payload1 = [
  {
    data: [
      [
        {
          name: "Handbag",
          url: "https://example.com/products/handbag",
          images: {
            small: "https://example.com/products/handbag/images/small.png",
            medium: "https://example.com/products/handbag/images/medium.png",
            large: "https://example.com/products/handbag/images/large.png",
          },
          color: "black",
          price: "$50.00",
        },
      ],
      [
        {
          name: "Shoes",
          url: "https://example.com/products/shoes",
          images: {
            small: "https://example.com/products/shoes/images/small.png",
            medium: "https://example.com/products/shoes/images/medium.png",
            large: "https://example.com/products/shoes/images/large.png",
          },
          color: "red",
          price: "$35.00",
        },
      ],
    ],
  },
];

const payload2 = [
  [
    {
      product_id: "000001",
      meta: {
        en_US: {
          product_name: "Handbag",
          product_urls: ["https://example.com/products/handbag"],
          product_image_small: "https://example.com/products/handbag/images/small.png",
          product_image_medium: "https://example.com/products/handbag/images/medium.png",
          product_image_large: "https://example.com/products/handbag/images/large.png",
          product_price_cents: 5000,
          product_custom_attributes: {
            product_price_string: "$50.00",
            product_color: "black",
          },
        },
      },
    },
    {
      product_id: "000002",
      meta: {
        en_US: {
          product_name: "Shoes",
          product_urls: ["https://example.com/products/shoes"],
          product_image_small: "https://example.com/products/shoes/images/small.png",
          product_image_medium: "https://example.com/products/shoes/images/medium.png",
          product_image_large: "https://example.com/products/shoes/images/large.png",
          product_price_cents: 3500,
          product_custom_attributes: {
            product_price_string: "$35.00",
            product_color: "red",
          },
        },
      },
    },
  ],
];

// main function
const parseProducts = (data, config) => {
    const saneData = config.array ? data[0] : data[0].data;
    return saneData.map(product => {
        return [
            { type: 'TEXT',  value: getText(product, config) },   // 'Handbag'
            { type: 'URL',   value: getUrl(product, config) },    // 'https://example.com/products/handbag'
            { type: 'IMAGE', value: getImage(product, config) },  // 'https://example.com/products/handbag/images/medium.png'
            { type: 'TEXT',  value: getPrice(product, config) }   // '$50.00'
        ]
    });
};

const getText = (product, config) => {
    if (Array.isArray(product)) {
        return product[0].name;
    }
    const meta = product?.meta[config.lang];
    return meta?.product_name;
}

const getUrl = (product, config) => {
    if (Array.isArray(product)) {
        return product[0].url;
    }
    const meta = product?.meta[config.lang];
    return meta?.product_urls[0];
}

const getImage = (product, config) => {
    if (Array.isArray(product)) {
        return product[0].images.medium;
    }
    const meta = product?.meta[config.lang];
    return meta?.product_image_medium;
}

const getPrice = (product, config) => {
    if (Array.isArray(product)) {
        return product[0].price;
    }
    const meta = product?.meta[config.lang];
    return meta?.product_custom_attributes.product_price_string;
}

// Test
console.log(parseProducts(payload1, { array: 0 }));
console.log(parseProducts(payload2, { array: 1, lang: "en_US" }));

Answer from Joel Peltonen on Stack Overflow
🌐
Medium
samedesilva.medium.com › how-to-create-a-nested-json-object-payload-with-an-array-using-java-map-and-pass-it-as-the-payload-2aa0611fa2b3
How to create a Nested JSON Object payload and pass it as the payload of Playwright Java API post request. | by Sameera De Silva | Medium
December 13, 2023 - package com.payloads; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; public class CreatedNestedJSONObjectPayloadUsingJavaMapWithArray { public static void main(String[] args) { // Create a Gson instance Gson gson = new Gson(); // Create the main JSON object JsonObject mainJsonObjectPayload = new JsonObject(); // Add properties to the main JSON object since id doesn't have child can add directly. mainJsonObjectPayload.addProperty("id", 0); // Create and add the nested "category" JSON object since category has two children as id,name JsonObject categoryObject = new JsonObject(); categoryObject.addProperty("id", 0); categoryObject.addProperty("name", "string"); //After that add category to main object as a simple key and value.
Top answer
1 of 1
1

EDIT: Whoops, forgot the config

Try to write the most efficient and concise code

Sure... for me "efficient" means "understandable" - if they wanted performance they should have said "performant" isntead :). And concise code... well, that doesn't always mean quality code, so I'll try to balance.

My approach would be to at least somewhat unify the data, then use smaller functions and deal with the super confusing data structure there.

If this was a very big project and a more generic project would actually be needed, only then would I consider making anything generic with such data. Often it's not actually necessary and just introduces a whole lot of complexity.

const payload1 = [
  {
    data: [
      [
        {
          name: "Handbag",
          url: "https://example.com/products/handbag",
          images: {
            small: "https://example.com/products/handbag/images/small.png",
            medium: "https://example.com/products/handbag/images/medium.png",
            large: "https://example.com/products/handbag/images/large.png",
          },
          color: "black",
          price: "$50.00",
        },
      ],
      [
        {
          name: "Shoes",
          url: "https://example.com/products/shoes",
          images: {
            small: "https://example.com/products/shoes/images/small.png",
            medium: "https://example.com/products/shoes/images/medium.png",
            large: "https://example.com/products/shoes/images/large.png",
          },
          color: "red",
          price: "$35.00",
        },
      ],
    ],
  },
];

const payload2 = [
  [
    {
      product_id: "000001",
      meta: {
        en_US: {
          product_name: "Handbag",
          product_urls: ["https://example.com/products/handbag"],
          product_image_small: "https://example.com/products/handbag/images/small.png",
          product_image_medium: "https://example.com/products/handbag/images/medium.png",
          product_image_large: "https://example.com/products/handbag/images/large.png",
          product_price_cents: 5000,
          product_custom_attributes: {
            product_price_string: "$50.00",
            product_color: "black",
          },
        },
      },
    },
    {
      product_id: "000002",
      meta: {
        en_US: {
          product_name: "Shoes",
          product_urls: ["https://example.com/products/shoes"],
          product_image_small: "https://example.com/products/shoes/images/small.png",
          product_image_medium: "https://example.com/products/shoes/images/medium.png",
          product_image_large: "https://example.com/products/shoes/images/large.png",
          product_price_cents: 3500,
          product_custom_attributes: {
            product_price_string: "$35.00",
            product_color: "red",
          },
        },
      },
    },
  ],
];

// main function
const parseProducts = (data, config) => {
    const saneData = config.array ? data[0] : data[0].data;
    return saneData.map(product => {
        return [
            { type: 'TEXT',  value: getText(product, config) },   // 'Handbag'
            { type: 'URL',   value: getUrl(product, config) },    // 'https://example.com/products/handbag'
            { type: 'IMAGE', value: getImage(product, config) },  // 'https://example.com/products/handbag/images/medium.png'
            { type: 'TEXT',  value: getPrice(product, config) }   // '$50.00'
        ]
    });
};

const getText = (product, config) => {
    if (Array.isArray(product)) {
        return product[0].name;
    }
    const meta = product?.meta[config.lang];
    return meta?.product_name;
}

const getUrl = (product, config) => {
    if (Array.isArray(product)) {
        return product[0].url;
    }
    const meta = product?.meta[config.lang];
    return meta?.product_urls[0];
}

const getImage = (product, config) => {
    if (Array.isArray(product)) {
        return product[0].images.medium;
    }
    const meta = product?.meta[config.lang];
    return meta?.product_image_medium;
}

const getPrice = (product, config) => {
    if (Array.isArray(product)) {
        return product[0].price;
    }
    const meta = product?.meta[config.lang];
    return meta?.product_custom_attributes.product_price_string;
}

// Test
console.log(parseProducts(payload1, { array: 0 }));
console.log(parseProducts(payload2, { array: 1, lang: "en_US" }));

🌐
QA Automation Expert
qaautomation.expert › tag › nested-json-payload
Nested JSON Payload – QA Automation Expert
So, we will split this into small parts or objects. So basically, we can split the above JSON into 4 parts – Employee, Contractors, CompanyPFDetails, and NestedPOJODemo. companyName, companyEmailId, companyNumber, and companyAddress are 1:1 mapping in the payload.
🌐
JDELIST
jdelist.com › home › forums › jd edwards® enterprise one / one world discussions › jd edwards® orchestrator
E9.2 - Create nested multi-level JSON payload | JDELIST.com - JD Edwards ERP Forums
April 23, 2024 - **Data Loading**: Load the input JSON into a Python dictionary. 2. **Data Structure Preparation**: Create a template for the output JSON based on the structure you provided.
🌐
Adobe
opensource.adobe.com › Spry › samples › data_region › JSONDataSetSample.html
JSON Data Set Sample
Doing that with the data set used in Example 10 would require some JavaScript logic embedded in spry attribute conditionals to control when things showed up. A simpler approach would be to use NestedJSONDataSets. In this example we use the same JSON data used in Example 10, but we will use 2 nested JSON data sets to track the "batter" and "topping" data.
🌐
Stack Overflow
stackoverflow.com › questions › 58946051 › construct-a-json-payload-in-javascript
Construct a JSON Payload in javascript - Stack Overflow
My requirement is slightly more different, so I submit a form and based on the inputs given in the form I have to construct the JSON. The inputs in the form contain both keys and values. So have to build based on it. For example: var env = current.variables.vpc_environment_type; var alliance = current.variables.alliance_business_unit; var team = current.variables.alliance_segment_team_name_df_ingestion; var project = current.variables.project_name_gcp_df_ingestion_npe; Those variables declared will store some keys and I have to construct a JSON based on them.
Find elsewhere
🌐
EasyMorph Community
community.easymorph.com › t › post-request-with-nested-json-body-how-can-i-generate-it › 2933
POST Request with nested JSON Body - how can I generate it? - EasyMorph Community - Data preparation professionals and enthusiasts
July 30, 2021 - Hello, I'm trying to send data through a JSON Body, however I think that the API receiving the data is expecting a nested JSON, with the text "inputs" in the first level, and the inputs themselves in the second level: More details: https://developer.salesforce.com/docs/atlas.en-us.api_action.meta/api_action/actions_obj_custom_notification.htm So in EasyMorph, I created the following action: However, here I can't include the first JSON level ("inputs"). At the moment this is an "Iterate...
Top answer
1 of 2
2

That first option works fine but a nested object needs its own subquery. Here is an example on the emp/dept sample dataset. It selects from dual since the outer level is a single key that has the all rows in an array, but that can also be from a table like you're doing.

SELECT JSON_OBJECT (
         KEY 'departments' VALUE (
           SELECT JSON_ARRAYAGG(
                    JSON_OBJECT (
                      KEY 'department_name' VALUE d.dname,
                      KEY 'department_no' VALUE d.deptno,
                      KEY 'employees' VALUE (
                        SELECT JSON_ARRAYAGG (
                                 JSON_OBJECT(
                                   KEY 'employee_number' VALUE e.empno,
                                   KEY 'employee_name' VALUE e.ename
                                 )
                               )
                        FROM   emp e
                        WHERE  e.deptno = d.deptno
                      )
                    )
                  )
           FROM   dept d
         )
       ) AS departments
FROM   dual;

{
  "departments": [
    {
      "department_name": "ACCOUNTING",
      "department_no": 10,
      "employees": [
        {
          "employee_number": 7839,
          "employee_name": "KING"
        },
        {
          "employee_number": 7782,
          "employee_name": "CLARK"
        },
        {
          "employee_number": 7934,
          "employee_name": "MILLER"
        }
      ]
    },
    {
      "department_name": "RESEARCH",
      "department_no": 20,
      "employees": [
        {
          "employee_number": 7566,
          "employee_name": "JONES"
        },
        {
          "employee_number": 7788,
          "employee_name": "SCOTT"
        },
        {
          "employee_number": 7902,
          "employee_name": "FORD"
        },
        {
          "employee_number": 7369,
          "employee_name": "SMITH"
        },
        {
          "employee_number": 7876,
          "employee_name": "ADAMS"
        }
      ]
    },
    {
      "department_name": "SALES",
      "department_no": 30,
      "employees": [
        {
          "employee_number": 7698,
          "employee_name": "BLAKE"
        },
        {
          "employee_number": 7499,
          "employee_name": "ALLEN"
        },
        {
          "employee_number": 7521,
          "employee_name": "WARD"
        },
        {
          "employee_number": 7654,
          "employee_name": "MARTIN"
        },
        {
          "employee_number": 7844,
          "employee_name": "TURNER"
        },
        {
          "employee_number": 7900,
          "employee_name": "JAMES"
        }
      ]
    },
    {
      "department_name": "OPERATIONS",
      "department_no": 40,
      "employees": null
    }
  ]
}
2 of 2
0

You need to use a GROUP BY clause when you have aggregated and non-aggregated columns. Given your structure you would want to use correlated sub-queries and JSON_ARRAY for the inner-most object (rather than JSON_ARRAYAGG, since you only have a single object and are not aggregating anything) :

SELECT JSON_OBJECT(
         KEY 'InvoiceNumber'   VALUE  h.document_id,
         KEY 'InvoiceCurrency' VALUE  'USD',
         KEY 'InvoiceAmount'   VALUE  h.amount,
         KEY 'InvoiceDate'     VALUE  h.trx_date,
         KEY 'BusinessUnit'    VALUE  'ABCD Corp',
         KEY 'Supplier'        VALUE  'NASA',
         KEY 'SupplierSite'    VALUE  'PHYSICAL',
         KEY 'InvoiceGroup'    VALUE  'MoonLander',
         KEY 'Description'     VALUE  'Some Description',
         KEY 'invoiceLines'    VALUE  (
           SELECT JSON_ARRAYAGG(
                    JSON_OBJECT(
                      KEY 'LineNumber' VALUE t.line_id,
                      KEY 'LineAmount' VALUE t.line_Value,
                      KEY 'invoiceDistributions' VALUE JSON_ARRAY(
                        JSON_OBJECT(
                          KEY 'DistributionLineNumber' VALUE t.line_id,
                          KEY 'DistributionLineType'   VALUE 'Item',
                          KEY 'DistributionAmount'     VALUE  t.line_Value
                        )
                      )
                    )
                  )
           FROM   XXRR_LINE_STG t
           WHERE  t.document_id = h.document_id
         )
       ) AS json_value
--INTO   aCLOB
FROM   XXRR_HDR_STG h
WHERE  h.document_id = 543210;

Which, for the sample data:

CREATE TABLE xxrr_hdr_stg(document_id, amount, trx_date) AS
SELECT 543210, 100, SYSDATE FROM DUAL

CREATE TABLE xxrr_line_stg(document_id, line_id, line_value) AS
SELECT 543210, 1, 2112.75 FROM DUAL;

Outputs:

JSON_VALUE
{"InvoiceNumber":543210,"InvoiceCurrency":"USD","InvoiceAmount":100,"InvoiceDate":"2023-09-05T09:07:51","BusinessUnit":"ABCD Corp","Supplier":"NASA","SupplierSite":"PHYSICAL","InvoiceGroup":"MoonLander","Description":"Some Description","invoiceLines":[{"LineNumber":1,"LineAmount":2112.75,"invoiceDistributions":[{"DistributionLineNumber":1,"DistributionLineType":"Item","DistributionAmount":2112.75}]}]}

fiddle

🌐
ReqBin
reqbin.com › req › 2xhbguy8 › json-payload-example
How do I send JSON Payload to the server?
... POST /echo/post/json HTTP/1.1 Host: reqbin.com Accept: application/json Content-Type: application/json Content-Length: 52 { "Id": 78912, "Quantity": 1, "Price": 18.00 } ... JavaScript Object Notation (JSON) is a standard text format for ...
🌐
IBM
ibm.com › docs › en › db2 › 11.5.x
JSON nested objects
Objects can be nested inside other objects. Each nested object must have a unique access path.
🌐
Campaign
developer.goacoustic.com › acoustic-campaign › docs › payload-examples
Payload examples
For example if the user want to create a custom action called “add-to-cal” then this is an example payload: JSON · { "type": "add-to-cal", "value": "Add To Calendar", "date": "8/8/2024", "recipients": [ "[email protected]", "[email protected]" ...
Top answer
1 of 5
25

The first code is an example of Javascript code, which is similar, however not JSON. JSON would not have 1) comments and 2) the var keyword

You don't have any comments in your JSON, but you should remove the var and start like this:

Copyorders: {

The [{}] notation means "object in an array" and is not what you need everywhere. It is not an error, but it's too complicated for some purposes. AssociatedDrug should work well as an object:

Copy"associatedDrug": {
                "name":"asprin",
                "dose":"",
                "strength":"500 mg"
          }

Also, the empty object labs should be filled with something.

Other than that, your code is okay. You can either paste it into javascript, or use the JSON.parse() method, or any other parsing method (please don't use eval)

Update 2 answered:

Copyobj.problems[0].Diabetes[0].medications[0].medicationsClasses[0].className[0].associatedDrug[0].name

returns 'aspirin'. It is however better suited for foreaches everywhere

2 of 5
19

I successfully solved my problem. Here is my code:

The complex JSON object:

Copy   {
    "medications":[{
            "aceInhibitors":[{
                "name":"lisinopril",
                "strength":"10 mg Tab",
                "dose":"1 tab",
                "route":"PO",
                "sig":"daily",
                "pillCount":"#90",
                "refills":"Refill 3"
            }],
            "antianginal":[{
                "name":"nitroglycerin",
                "strength":"0.4 mg Sublingual Tab",
                "dose":"1 tab",
                "route":"SL",
                "sig":"q15min PRN",
                "pillCount":"#30",
                "refills":"Refill 1"
            }],
            "anticoagulants":[{
                "name":"warfarin sodium",
                "strength":"3 mg Tab",
                "dose":"1 tab",
                "route":"PO",
                "sig":"daily",
                "pillCount":"#90",
                "refills":"Refill 3"
            }],
            "betaBlocker":[{
                "name":"metoprolol tartrate",
                "strength":"25 mg Tab",
                "dose":"1 tab",
                "route":"PO",
                "sig":"daily",
                "pillCount":"#90",
                "refills":"Refill 3"
            }],
            "diuretic":[{
                "name":"furosemide",
                "strength":"40 mg Tab",
                "dose":"1 tab",
                "route":"PO",
                "sig":"daily",
                "pillCount":"#90",
                "refills":"Refill 3"
            }],
            "mineral":[{
                "name":"potassium chloride ER",
                "strength":"10 mEq Tab",
                "dose":"1 tab",
                "route":"PO",
                "sig":"daily",
                "pillCount":"#90",
                "refills":"Refill 3"
            }]
        }
    ],
    "labs":[{
        "name":"Arterial Blood Gas",
        "time":"Today",
        "location":"Main Hospital Lab"      
        },
        {
        "name":"BMP",
        "time":"Today",
        "location":"Primary Care Clinic"    
        },
        {
        "name":"BNP",
        "time":"3 Weeks",
        "location":"Primary Care Clinic"    
        },
        {
        "name":"BUN",
        "time":"1 Year",
        "location":"Primary Care Clinic"    
        },
        {
        "name":"Cardiac Enzymes",
        "time":"Today",
        "location":"Primary Care Clinic"    
        },
        {
        "name":"CBC",
        "time":"1 Year",
        "location":"Primary Care Clinic"    
        },
        {
        "name":"Creatinine",
        "time":"1 Year",
        "location":"Main Hospital Lab"  
        },
        {
        "name":"Electrolyte Panel",
        "time":"1 Year",
        "location":"Primary Care Clinic"    
        },
        {
        "name":"Glucose",
        "time":"1 Year",
        "location":"Main Hospital Lab"  
        },
        {
        "name":"PT/INR",
        "time":"3 Weeks",
        "location":"Primary Care Clinic"    
        },
        {
        "name":"PTT",
        "time":"3 Weeks",
        "location":"Coumadin Clinic"    
        },
        {
        "name":"TSH",
        "time":"1 Year",
        "location":"Primary Care Clinic"    
        }
    ],
    "imaging":[{
        "name":"Chest X-Ray",
        "time":"Today",
        "location":"Main Hospital Radiology"    
        },
        {
        "name":"Chest X-Ray",
        "time":"Today",
        "location":"Main Hospital Radiology"    
        },
        {
        "name":"Chest X-Ray",
        "time":"Today",
        "location":"Main Hospital Radiology"    
        }
    ]
}

The jQuery code to grab the data and display it on my webpage:

Copy$(document).ready(function() {
var items = [];

$.getJSON('labOrders.json', function(json) {
  $.each(json.medications, function(index, orders) {
    $.each(this, function() {
        $.each(this, function() {
            items.push('<div class="row">'+this.name+"\t"+this.strength+"\t"+this.dose+"\t"+this.route+"\t"+this.sig+"\t"+this.pillCount+"\t"+this.refills+'</div>'+"\n");
        });
    });
  });

  $('<div>', {
    "class":'loaded',
    html:items.join('')
  }).appendTo("body");

});

});

🌐
Postman
community.postman.com › help hub
How can I target nested objects from a JSON data file? - Help Hub - Postman Community
December 12, 2017 - Given I have the below JSON data file [ { "email": "test@test.com", "address": { "addressLine1": "123 Fake Street", "county": "Something", "postcode": "BA11 4PM" } } ] And I want to use the data file in my POST body each iteration (I know, I ...
🌐
GitHub
github.com › strongloop › loopback-next › issues › 5611
How do I validate a nested JSON api request body payload using ...
April 4, 2020 - I am trying to validate a nested JSON payload using models in loopback4. Loopback4 is able to validate the name and description property. However, it isn't validating the contact.type or contact.number property. This is an example of wha...
Published   May 30, 2020