Faisal
Faisal

Reputation: 168

In jsonpath how to put new value for nested json object?

I have Json like this:

{
  "attachments": [
    "string"
  ],
  "contact": {
    "name": "Mahesh"
  },
  "contactCode": "C-0000001",
  "journalEntryCode": "JE-0000002",
  "linkedDocuments": [
    {
      "contactCode": "C-0000001",
      "documentCode": "0000018",
      "documentItemDetails": [
        {
          "productCode": "P-0000001"
        }
      ],
      "documentType": "QUOTATION",
    }
  ],
  "taxAmount": 2.322,
  "totalAmount": 12.322,
  "unitPriceGstInclusive": false
}

Under "linkedDocuments" I want to put/replace new value "1234" for "documentCode" I tried following by taking json path:

JSONObject requestParams = Utilities.readJSON("createInvoiceFromQuote.json");

        requestParams.put("$.linkedDocuments[*].documentCode", "1234");

But it is just creating a new field "$.linkedDocuments[*].documentCode" at end of json like this

{
  "attachments": [
    "string"
  ],
  "contact": {
    "name": "Mahesh"
  },
  "contactCode": "C-0000001",
  "journalEntryCode": "JE-0000002",
  "linkedDocuments": [
    {
      "contactCode": "C-0000001",
      "documentCode": "0000018",
      "documentItemDetails": [
        {
          "productCode": "P-0000001"
        }
      ],
      "documentType": "QUOTATION",
    }
  ],
  "taxAmount": 2.322,
  "totalAmount": 12.322,
  "unitPriceGstInclusive": false
  "$.linkedDocuments[*].documentCode":"1234"
}

It should be like this

{
  "attachments": [
    "string"
  ],
  "contact": {
    "name": "Mahesh"
  },
  "contactCode": "C-0000001",
  "journalEntryCode": "JE-0000002",
  "linkedDocuments": [
    {
      "contactCode": "C-0000001",
      "documentCode": "1234",
      "documentItemDetails": [
        {
          "productCode": "P-0000001"
        }
      ],
      "documentType": "QUOTATION",
    }
  ],
  "taxAmount": 2.322,
  "totalAmount": 12.322,
  "unitPriceGstInclusive": false
}

How to I put/replace nested field value using java?

Upvotes: 1

Views: 1950

Answers (2)

Vishakha Lall
Vishakha Lall

Reputation: 1324

You need a minor correction in your code.

JSONObject requestParams = Utilities.readJSON("createInvoiceFromQuote.json");
JSONArray linkedDocuments = requestParams.getJSONArray("linkedDocuments");
JSONObject document = linkedDocuments.getJSONObject(0) 
document.put("documentCode", 1234);

Output

{
  "attachments": [
    "string"
  ],
  "contact": {
    "name": "Mahesh"
  },
  "contactCode": "C-0000001",
  "journalEntryCode": "JE-0000002",
  "linkedDocuments": [
    {
      "contactCode": "C-0000001",
      "documentCode": "1234",
      "documentItemDetails": [
        {
          "productCode": "P-0000001"
        }
      ],
      "documentType": "QUOTATION",
    }
  ],
  "taxAmount": 2.322,
  "totalAmount": 12.322,
  "unitPriceGstInclusive": false
}

Upvotes: 1

hagarwal
hagarwal

Reputation: 1163

Since linkedDocuments is an JSONArray of JSONObject therefore "$.linkedDocuments[*].documentCode" key will not work as expected. To update any key of the JSONObject in JSONArray, access the JOSNArray and iterate on all the JSONObject and then perform the update.

String str = "{\"attachments\":[\"string\"],\"contact\":{\"name\":\"Mahesh\"},\"contactCode\":\"C-0000001\",\"journalEntryCode\":\"JE-0000002\",\"linkedDocuments\":[{\"contactCode\":\"C-0000001\",\"documentCode\":\"0000018\",\"documentItemDetails\":[{\"productCode\":\"P-0000001\"}],\"documentType\":\"QUOTATION\"},{\"contactCode\":\"C-0000002\",\"documentCode\":\"0000019\",\"documentItemDetails\":[{\"productCode\":\"P-0000002\"}],\"documentType\":\"QUOTATION\"}],\"taxAmount\":2.322,\"totalAmount\":12.322,\"unitPriceGstInclusive\":false}";

JSONObject jsonObject = new JSONObject(str);

JSONArray array = jsonObject.getJSONArray("linkedDocuments");
array.getJSONObject(0).put("documentCode", 1234);
System.out.println(jsonObject);

Output:

{
  "linkedDocuments": [
    {
      "documentCode": 1234,
      "contactCode": "C-0000001",
      "documentType": "QUOTATION",
      "documentItemDetails": [
        {
          "productCode": "P-0000001"
        }
      ]
    },
    {
      "documentCode": "0000019",
      "contactCode": "C-0000002",
      "documentType": "QUOTATION",
      "documentItemDetails": [
        {
          "productCode": "P-0000002"
        }
      ]
    }
  ],
  "totalAmount": 12.322,
  "attachments": [
    "string"
  ],
  "contactCode": "C-0000001",
  "contact": {
    "name": "Mahesh"
  },
  "journalEntryCode": "JE-0000002",
  "taxAmount": 2.322,
  "unitPriceGstInclusive": false
}

Upvotes: 3

Related Questions