I'd like to remove from an array objects that pass a test against a property'
Assume we have the following array:
[
{
"path": "a/b/v1-another"
},
{
"path": "a/b/v1"
}
]
I'd like to remove from the array the object that match the test a/b/* against path property
Like this:
jq '.[]|select(.path|startswith("a/b/")|not)' file.json
Related
I want to know the name who purchased items "abc" and "def" from the bellow json data.
(Expected result is "tom")
Please tell me how to do using JsonPath.
[
{
"name": "tom",
"purchased": [
{
"name": "abc"
},
{
"name": "def"
}
]
},
{
"name": "bob",
"purchased": [
{
"name": "xyz"
}
]
}
]
If you are doing this in Java you could select the name of the buyer like this:
$..[?(#.purchased && #..name contains 'abc' && #..name contains 'def' )].name
In JavaScript, you can use this a query like this:
$.[?(#.purchased && #.purchased.some(e => e.name==='abc') && #.purchased.some(e => e.name==='def') )].name
Both queries use a similar approach:
We filter for a purchased-property, then use a function to search the keys in the map for certain values
Jayway's JsonPath offers a contains function to do so;
In JavaScript we leverage script evaluation to search the map/dict for our key/values
So, the .some() is not a JsonPath function but some modern JavaScript (:D).
This is kind of a workaround in both cases but it gets the job done.
You can test both online here. Select the tab JayWay and click Go for the first query, switch to tab Goessner to run the JavaScript sample (you can run the second here as well).
I would like to return the whole JSON if a condition is matched.
Test Json:
{
"EVENTID": 2624367601,
"RECEIVERNAME": "CM.MYHR",
"SENDERNAME": "CM.EIS.CF1",
"AGREEMENTNAME": null
}
I keep trying in https://jsonpath.curiousconcept.com, but couldn't figure it out.
I thought the following expression should work, but it always return empty.
$.[?(#.SENDERNAME==CM.EIS.CF1)]
Please help.
You only have a fragment of a json. Try it like this:
{
"events": [
{
"EVENTID": 2624367601,
"RECEIVERNAME": "CM.MYHR",
"SENDERNAME": "CM.EIS.CF1",
"AGREEMENTNAME": null
}
]
}
And use:
$.*[?(#.SENDERNAME=='CM.EIS.CF1')]
If you want while json as it is from input, then as per my understanding you have to use below expression:
$.
I have tested it in https://jsonpath.curiousconcept.com/
I need to modify an element --an array-- (e.g.: "group-xyz") within a nested object in a JSON tree using JQ but once that's done then I need the entire object back with the modified data.
The goal is to update a JSON tree and save it in full.
e.g.: add array element, empty array, etc.
{
"group-abc": {"users": ["tina.turner"]},
"group-def": {"users": ["someone.else"]},
"group-xyz": {"users": ["that.thing"]
}
Then I am interested in returning an object like this:
{
"group-abc": {"users": ["tina.turner"]},
"group-def": {"users": []},
"group-xyz": {"users": ["that.thing","well.done"]
}
I have changed my requirements to fit a more complex form. To add a user to any of these groups' users this is what I did:
jq '. |= map( if ( .group=="abc") then .users+=["final.answer",] else . end)' source.json
which produced a result
[
{
"group": "abc",
"users": [
"user1",
"user2",
"final.answer"
]
},
{
"group": "def",
"users": [
"user4",
"user5"
]
}
]
How can I have this structure in a query string?
"properties": {
"list": [
{
"label": "bye",
"value": "world"
},
{
"label": "hello",
"value": "mars"
}
]
}
I've tried it with properties[][list][label]=bye&properties[][list][value]=world&properties[0][label]=hello&properties[0][value]=mars and also with properties[][list][label]=bye&properties[][list][value]=world&properties[][list][label]=hello&properties[][list][value]=mars, none of them worked. I built them in php with http_build_query.
I need to have this structure in a query string because I have to send the data along with some other stuff with POST to a PHP site.
I see two errors in your query string:
properties is an object, so there's no need to use [] to add elements.
list is an array, so you must use numeric indexes in the query string.
The correct query string is:
?properties[list][0][label]=bye
&properties[list][0][value]=world
&properties[list][1][label]=hello
&properties[list][1][value]=mars
(multi-lined for readability)
How do I iterate through the Json object that has an array within it and places the description object in a variable that can be used in another function?
This is the Json scheme that is being pulled in with $regResult:
{
"errors": [
{
"code": "401.07.001",
"description": "Invalid Access Token",
"link": "https://developer.arity.com/registration-services/apis"
}
]
}
You have multiple options
just use return $registration_result; and pass it to the other function as a parameter
Declare your variable as global global $registration_result = drupal_json_decode($regResult);
Save the value to the database