52

I have the following JSON and I need to get the plain name value using JSONPath:

{
  "single" : {
    "id" : 1, 
    "name" : "Item name"
  }
}

Expression that I used is $.single.name but I always get an array:

[ "Item name" ]

instead of a string value ("Item name").

7 Answers 7

45

but I always get an array:

That is meant to happen. As you can read in this documentation, under 'Result' (almost at the bottom):

Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it.

So basically it will always return an array. If you need the data as an other type, e.g. a String in this case, you will have to do the conversion yourself I'm afraid.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I got to that conclusion. Reason why I asked is to see if I am missing a bit of JSONPath syntax that will let me get the first element of the resulting array, and not an entire array.
@Ilija Sadly no. However I checked the source code and it's not very complicated, perhaps you could make some modifications to have it return something else than an array when you want it to.
Yes, I ended up doing that (if path ends with ~first it will return first element of the resulting array). Hate doing things like that, but if there was no other way…
If you got here like me you probably just need to add [*] to the end of your JSONPath query. It flattened the array being returned for me.
I'm using jsonpath-plus npm package that has wrap property. You can read more about the implications here: npmjs.com/package/…
15

I was using the Java implementation of JSONPath and got to the very same issue. What worked for me was to add '[0]' to the json path string. So in your case:

$.single.name[0]

5 Comments

I ran into the similar problem but with expression. I wan to get from array single object and from that object single value, but this doesnt work: data[?(@.id==9617)][0].is_favorite this doesnt work either data[?(@.id==9617)].[0].is_favorite
Provide an example of your data first.
Here it is {state:{apiVersion:"V3_0",errorLevel:0,errorMessage:null,errorLink:null},data:[{id:94617,is_favorite:true,vip_sub_offers:[],vouchers:[{id:11613004,code:"F8JL2",offer:94617,subOffer:1613,validTo:"2016-05-20T23:59:59.000",state:"USED",stateCode:2,obtainType:"NORMAL",redirectUrl:null,phoneNumber:null,messageText:null,shopId:94614,assignTime:"2016-05-19T00:00:03.000",useTime:"2016-05-19T00:00:05.000"}]}]}
The Java implementation can't apply the [0] after using a predicate expression. It's apparently out of spec: github.com/json-path/JsonPath/issues/272
As far as is read somewhere, there is a Java implementation in fastjason, that can handle the [0]. The implementation from Jayway can't.
3

It depends of the library implementations in which some of them are rigid and others offer an option to make the developer smile.

Nodejs

For example in nodejs there is a npm module : https://www.npmjs.com/package/jsonpath

Which have a method called value, which does exactly what we need

jp.value(obj, pathExpression[, newValue])

Returns the value of the first element matching pathExpression. If newValue is provided, sets the value of the first matching element and returns the new value.

Java

String json = "...";
Object document = ...parse(json);
String author0 = JsonPath.read(document, "$.store.book[0].author");

As you can see author0 is a string, not an array

Source: https://github.com/json-path/JsonPath

Comments

0

In case you got this error

"Unable to get a scalar value for expression $.yourfield"

You have just to configure the EvaluateJsonPath processor by changing the return type property value to 'json' instead of 'auto-detect'

enter image description here

Comments

0

In an Azure Bicep template, value worked for me, in extracting a single parameter from a jsonc parameter file: $.parameters.aksAgentPoolIdentityPrincipalId.value

One could fathom a guess that in my case this is used as the implementation:
https://github.com/json-path/JsonPath

Comments

0

In javascript it is possible to get a single value addressed by a path from a json object using the npm library jsonpath-plus

Use the option { wrap: false } to avoid the array wrapper.

import { JSONPath } from 'jsonpath-plus';

const json = {
  single : {
    id : 1, 
    name : "Item name"
  }
}
const path = '$.single.name'

console.log(JSONPath({json, path: path, wrap: false})) // => Item name

Comments

0

jsonpath returns an object of net.minidev.json.JSONArray.JSONArray from the read method. This class has size() and get(int) methods which can be used to retrieve specific values. The below example prints the 0th element as retrieved.

JSONArray o=docCtx.read("json path ...");
System.out.println(o.get(0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.