How to remove square bracket and double quotes from extracted json object

I have one json array object and i need to extract one particular value out of it, i used JSONPATH to extract it and the output is coming like this ["vIMS"] but i need to remove this square bracket and double quotes and i want value as vIMS. How can i achieve this.

Below is the JSON file :

{
  "externalId": "optusONAP1",
  "priority": "1",
  "category": "Consumer",
  "requestedStartDate": "2018-09-26T20:04:11.299Z",
  "requestedCompletionDate": "2018-09-26T20:04:11.299Z",
  "@baseType": null,
  "@type": null,
  "@schemaLocation": null,
  "relatedParty": [
    {
  "id": "6490",
  "href": null,
      "role": "ONAPcustomer",
      "name": "Jean Pontus",
      "@refferredType": "individual"
    }
  ],
  "orderRelationship": null,
  "orderItem": [
    {
  "id": "1",
  "action": "add",
  "@type": null,
  "@schemaLocation": null,
  "@baseType": null,
  "orderItemRelationship": [],
  "service": {
    "id": "vIMS", 
"href": null,
"name": "vIMS",
"serviceState": "active",
"@type": null,
"@schemaLocation": null,
"serviceCharacteristic": null,
"serviceRelationship": null,
"relatedParty": null,
"serviceSpecification": {
       "id": "6e2362bb-6dd0-41d4-8af7-4c1d4ebb54c4",
  "href": null,
  "name": null,
  "version": null,
  "targetServiceSchema": null,
  "@type": null,
  "@schemaLocation": null,
  "@baseType": null
}
  }
}
  ]
}

I need to extract the value of id which is vIMS and i could extract the value as ["vIMS"] using this JSON PATH = $.orderItem..service.id

How can i get rid of square bracket and double quotes.

Solved Solved
1 2 29.9K
1 ACCEPTED SOLUTION

This jsonpath works for me.

$.orderItem[0].service.id

The reason you need the [0] indexer there, is that orderItem is an array. If you omit the indexer, then with $.orderItem, you refer to the array of orderItem things. There is only one, but it's still an array, an array of one. Therefore extracting the service.id of those things gives you an array of ONE service id. which is why you get ["vIMS"].

If you want just ONE, and not an array of one, then you need to specify which of the orderItem things you want to extract from. I chose the zeroth (first) one, with the indexer.

If the orderItem array in the JSON actually will contain more than one item sometimes, then just selecting the zeroth item won't be satisfactory, probably. You'll have to come up with a different plan for that. It depends on what you want to do with the service.id thing.


And just in case you were not aware: you can do jsonpath within an AssignMessage policy, like this:

<AssignMessage name='AV-ExtractedInfo'>
  <AssignVariable>
    <Name>json_path_1</Name>
    <Value>$.orderItem[0].service.id</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>assigned</Name>
    <Value>BADDBEEF</Value>
    <Template>{jsonPath(json_path_1,the_json)}</Template>
  </AssignVariable>
</AssignMessage>

View solution in original post

2 REPLIES 2

Hi @Ankit Goel, we can make use of JS policy after JSON Extarct Policy.

I guess this can be done in multiple ways, I am using replace fucntion,

var extracted_json = ["vIMS"]; // use context.getVariable("<<extracted-variable>>");
modifiedJson = JSON.stringify(extracted_json).replace(/[\[\]"]+/g,"");
context.setVariable("final-variable", modifiedJson)

In some scenarios, we can avoid JS and use Message Templates which can do a lot of stuff. Explore message templates,

https://docs.apigee.com/api-platform/reference/message-template-intro#replace-all-function

This jsonpath works for me.

$.orderItem[0].service.id

The reason you need the [0] indexer there, is that orderItem is an array. If you omit the indexer, then with $.orderItem, you refer to the array of orderItem things. There is only one, but it's still an array, an array of one. Therefore extracting the service.id of those things gives you an array of ONE service id. which is why you get ["vIMS"].

If you want just ONE, and not an array of one, then you need to specify which of the orderItem things you want to extract from. I chose the zeroth (first) one, with the indexer.

If the orderItem array in the JSON actually will contain more than one item sometimes, then just selecting the zeroth item won't be satisfactory, probably. You'll have to come up with a different plan for that. It depends on what you want to do with the service.id thing.


And just in case you were not aware: you can do jsonpath within an AssignMessage policy, like this:

<AssignMessage name='AV-ExtractedInfo'>
  <AssignVariable>
    <Name>json_path_1</Name>
    <Value>$.orderItem[0].service.id</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>assigned</Name>
    <Value>BADDBEEF</Value>
    <Template>{jsonPath(json_path_1,the_json)}</Template>
  </AssignVariable>
</AssignMessage>