append values of objects inside array using jq - jq

I have an object which has an array of objects. I am willing to append two properties of each object inside the array and create a new key out of that. I am new to JQ and have tried various ways to do this, but not able to figure out. Need help.
Input:
{
"name": "Toyota",
"Model": "Innova",
"Details": [
{
"entry_day": "23",
"entry_month": "May"
},
{
"entry_day": "01",
"entry_month": "Jan"
}
]
}
Output I expect:
{
"name": "Toyota",
"Model": "Innova",
"Details": [
{
"entry_date": "23 May"
},
{
"entry_date": "01 Jan"
}
]
}

You need to use the update assignment operator |=:
jq '(.Details[]|={entry_date:"\(.entry_day) \(.entry_month)"})' input.json

Related

Using jq to get arrays where a key within the array is equal to a specific value?

I have been practicing with jq play to try to get all the arrays in a list where website is == "google" and create another json list from that.
https://jqplay.org/s/DKNC2mhOLq
jq: error (at :18): Cannot index array with string "website"
exit status 5
{
"items": [
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"2",
"website":"jingle"
}
]
Desired output:
[
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"1",
"website":"google"
}
]
how can I loop through arrays in a list and look for specific values for specific keys? Thanks for any help or ideas you can provide. I am a begginer with JSON and jq.
Enclose the select with a map, as you want to apply the filter to each array item individually while retaining the surrounding array structure.
jq '.items | map(select(.website == "google"))'
[
{
"name": "name1",
"id": "1",
"website": "google"
},
{
"name": "name1",
"id": "1",
"website": "google"
}
]
Demo

Add a single object to an array

So I have this json structure:
{
"dog": [
{
"name": "sam",
"age": "2"
},
{
"name": "billy",
"age": "5"
}
]
}
I've found that .dog[1] will return me the first object but not in the dog:[] array.
{
"name": "billy",
"age": "5"
}
and .[] |= .[$i] gives me an object:
{
"dog": {
"name": "billy",
"age": "5"
}
}
What I want is:
{
"dog": [
{
"name": "sam",
"age": "2"
}
]
}
I plan to use this in a bash script, and write out to multiple files like:
jq -r --argjson i "$i" '.[] |= .[$i]' "$1"
Instead of an integer index, use a slice. (Also, the array is 0-indexed, not 1-indexed.)
$ jq --argjson i 0 '{dog: .dog[$i:$i+1]}' < tmp.json
{
"dog": [
{
"name": "sam",
"age": "2"
}
]
}
As you are asking for an object, not a string, the -r option doesn't do anything.
Try
jq --argjson i 2 '.dog|=[.[$i-1]]'
The .dog|=[.[$index]] part modifies just the array dog and replaces it with an array of just the item at position $index. This has the benefit of preserving anything else that might be in the top-level object. We use $i-1 since you indicate you want to provide 1-based indices as input.
JSON arrays starts from zero. you should use index 0 instead of 1:
.dog[0]

How can I Extract a Large json Array in U-SQL and Include meta-data?

I am trying to extract a large amount of Data from a json document. There are about 1500 nodes per json document. When I attempt to load the body node I get the 128KB limit error. I have found a way to load the node but I have to go all the way down to the array list. JsonExtractor("body.nprobe.items[*]"); The issue I am having is that I cannot access any other part of the json document, I need to get the meta data, like: Id, SerialNumber etc. Should the json file be changed in some way? The data I need is 3 levels down. The json has be obfuscated and shortened, the real file is about 33K lines of formatted json about 1500 items with n-20 fields in each.
{
"headers": {
"pin": "12345",
"Type": "URJ201W-GNZGK",
"RTicks": "3345",
"SD": "211",
"Jov": "juju",
"Market": "Dal",
"Drst": "derre",
"Model": "qw22",
"DNum": "de34",
"API": "34f",
"Id": "821402444150002501"
},
"Id": "db5aacae3778",
"ModelType": "URJ",
"body": {
"uHeader": {
"ID": "821402444150002501",
"SerialNo": "ee028861",
"ServerName": "BRXTTY123"
},
"header": {
"form": 4,
"app": 0,
"Flg": 1,
"Version": 11056,
"uploadID": 1,
"uDate": "2016-04-14T18:29"
},
"nprobe": {
"items": [{
"purchaseDate": "2016-04-14T18:21:09",
"storeLoc": {
"latitude": 135.052335,
"longitude": 77.167005
},
"sr": {
"ticks": 3822,
"SkuId": "24",
"Data": {
"X": "0.00068",
"Y": "0.07246",
}
}
},
{
"purchaseDate": "2016-04-14T18:21:09",
"storeLoc": {
"latitude": 135.052335,
"longitude": 77.167005
},
"sr": {
"ticks": 3823,
"SkuId": "25",
"Data": {
"X": "0",
"Y": "2",
}
}
}]
}
},
"Messages": []
}
Thanks.
You'll have to use CROSS APPLY:
https://msdn.microsoft.com/en-us/library/azure/mt621307.aspx
and EXPLODE:
https://msdn.microsoft.com/en-us/library/azure/mt621306.aspx
See a worked out solution here:
https://github.com/algattik/USQLHackathon/blob/master/VS-Solution/USQLApplication/ciam-to-sqldw.usql
https://github.com/algattik/USQLHackathon/blob/master/Samples/Customer/customers%202016-08-10.json
-- IMPROVED ANSWER: --
As this solution will not work for you since your inner JSON is too large to fit in a string, you can parse the input twice:
DECLARE #input string = #"/so.json";
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
#meta =
EXTRACT Id string
FROM #input
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor();
#items =
EXTRACT purchaseDate string
FROM #input
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor("body.nprobe.items[*]");
#itemsFull =
SELECT Id,
purchaseDate
FROM #meta
CROSS JOIN #items;
OUTPUT #itemsFull
TO "/items_full.csv"
USING Outputters.Csv();

QJsonDocument::array() and QJsonDocument::object()

I am reading the QJsonDocument documentation and I use QJsonDocument with following line:
emit this->ueSignalNewDataArrivedPlaces(QJsonDocument::fromBinaryData(incomingData[0].toByteArray()));
and I do not understand, after this line, should I use QJsonDocument::array() or QJsonDocument::object(), i.e., in what situations does QJsonDocument create array and in what situations does create object?
A JSON array is an ordered list, it is written as:
[ <item1>, <item2>, <item3> ]
while a JSON object is a named list, written as:
{
<name1>: <item1>,
<name2>: <item2>
}
In Qt, a QJsonArray is equivalent to a QVariantList (QList<QVariant>) and a QJsonObject is equivalent to QVariantMap (QMap<QString, QVariant>).
Which one you have to use depends on the file you are parsing.
For instance, taking Wikipedia example:
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
You would use a QJsonArray to get the list of phoneNumbers, each element of phoneNumbers is a QJsonObject whith 2 named values: type and number.
If in your code you need to manipulate a JSON element but you do not know its type you can use QJsonValue, which is one of: QJsonObject, QJsonArray, bool, double or a QString

JSONPath first occurrence of "gatherer"

I have a json file like this:
{
"cards": [
{
"artist": "Steve Argyle",
"images": {
"mtgimage": "http://mtgimage.com/set/pMPR/ponder.jpg"
},
},
{
"artist": "Mark Tedin",
"images": {
"gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=139512",
"mtgimage": "http://mtgimage.com/set/LRW/ponder.jpg"
},
},
{
"artist": "Dan Scott",
"images": {
"gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=190159",
"mtgimage": "http://mtgimage.com/set/M10/ponder.jpg"
},
}
]
}
I would like to get the first "gatherer" link from it using JSONPath.
I tried "$..gatherer[0]" but that doesn't work. However "$..gatherer" does give both instances:
[
"http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=139512",
"http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=190159"
]
How do I get only the first one? (without getting the last one in code, so only using a jsonpath string.)
(Tested with http://jsonpath.curiousconcept.com/ and in my program.)
You may need an interim step to save the array
var gatherers = $..gatherer; //however this gets it, I'm not sure
var first = gatherers[0];
that may work.

Resources