{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • General
    • Edge/API Management
    • Developer Portal
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Analytics
    • Events
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • General /
avatar image
1
Question by Ankit Goel · Dec 05, 2018 at 04:43 AM · 328 Views jsonjson payloadjsonpath

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.

Comment
Add comment
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Close

2 Answers

· Add your answer
  • Sort: 
avatar image
0
Best Answer

Answer by Dino-at-Google   · Dec 05, 2018 at 05:48 PM

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>
Comment
Add comment · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Siddharth Barahalikar   · Dec 05, 2018 at 05:49 AM

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

Comment
Add comment · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 5 attachments (including images) can be used with a maximum of 5.0 MB each and 25.0 MB total.

Follow this Question

Answers Answers and Comments

52 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JSON payload not returned as expected. 3 Answers

JsonPath not working 1 Answer

not able to parse JSON file,parsing a json 1 Answer

json-ld response convert to regular json 2 Answers

Access JSON response from Service Callout in Javascript 1 Answer

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2019 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • General
  • Edge/API Management
  • Developer Portal
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Analytics
  • Events
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Members
  • Badges