I am attempting to parse out the following json array below. The activities object is a string and I have successfully been able to parse it out with the following in jq.
[
{
"id": 101,
"created_at": "1987-10-12 03:20:22.903",
"updated_at": "1987-10-12 01:20:36.019",
"activities": "{\"hero\": \"batman\", \"weapon\": \"sword\", \"ts\": null, \"remote\": \"1\",\"game\": \"joker\", \"image\": \"bold\"}"
},
{
"id": 102,
"created_at": "2022-10-12 11:20:22.903724",
"updated_at": "2022-10-12 11:20:36.019043",
"activities": "{\"hero\": \"superman\", \"weapon\": \"cape\", \"ts\": null, \"remote\": \"2\",\"game\": \"batman\", \"image\": \"kind\"}"
}
]
The following command allows me to parse out the string object but I haven't quite figured out how to get it back into the original array.
jq '.[].activities | fromjson?' test.json
Output:
{
"hero": "batman",
"weapon": "sword",
"ts": null,
"remote": "1",
"game": "joker",
"image": "bold"
}
{
"hero": "superman",
"weapon": "cape",
"ts": null,
"remote": "2",
"game": "batman",
"image": "kind"
}
I tried several variances of the += and |= operators to add back to the original without any luck.
jq '.[].activities |= fromjson? | select( . != "")'
Bypassed any empty values with the select command.
This yields no results for the activities object when I run it. It brings the original id, created_at, and updated_at forward but nothing else.
Appreciate any help you can offer. Thanks guys.
Preferred Output
[
{
"id": 101,
"created_at": "1987-10-12 03:20:22.903",
"updated_at": "1987-10-12 01:20:36.019",
"hero": "batman",
"weapon": "sword",
"ts": null,
"remote": "1",
"game": "joker",
"image": "bold"
},
{
"id": 102,
"created_at": "2022-10-12 11:20:22.903724",
"updated_at": "2022-10-12 11:20:36.019043",
"hero": "superman",
"weapon": "cape",
"ts": null,
"remote": "2",
"game": "batman",
"image": "kind"
}
]
Something like this should be enough:
map(del(.activities) + (.activities | fromjson))
Online demo
Here's a starting point, assigning a real JSON object to the activities key:
map(.activities |= fromjson)
Output:
[
{
"id": 101,
"created_at": "1987-10-12 03:20:22.903",
"updated_at": "1987-10-12 01:20:36.019",
"activities": {
"hero": "batman",
"weapon": "sword",
"ts": null,
"remote": "1",
"game": "joker",
"image": "bold"
}
},
{
"id": 102,
"created_at": "2022-10-12 11:20:22.903724",
"updated_at": "2022-10-12 11:20:36.019043",
"activities": {
"hero": "superman",
"weapon": "cape",
"ts": null,
"remote": "2",
"game": "batman",
"image": "kind"
}
}
]
And to merge the activities sub-object into its parent object, while deleting the old property:
map(. + (.activities | fromjson) | del(.activities))
Output:
[
{
"id": 101,
"created_at": "1987-10-12 03:20:22.903",
"updated_at": "1987-10-12 01:20:36.019",
"hero": "batman",
"weapon": "sword",
"ts": null,
"remote": "1",
"game": "joker",
"image": "bold"
},
{
"id": 102,
"created_at": "2022-10-12 11:20:22.903724",
"updated_at": "2022-10-12 11:20:36.019043",
"hero": "superman",
"weapon": "cape",
"ts": null,
"remote": "2",
"game": "batman",
"image": "kind"
}
]
I have the following JSON which have an element array_within_array in another array. So I want to pull both array_within_array element and upper array element(event_array) to root level using jq.
So here there are 3 events.
meal_selection
login
placed_order
Event placed_order have two sub-events in array. So after conversion there should be 4 events(1 from meal_selection, 1 from login and 2 from placed_order). these all should be on the same level.
Here is the JSON
{
"region": "USA",
"user_id": "123",
"event_array": [{
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "meal_selection",
"created_at": "2021-08-13 01:28:57"
},
{
"event_name": "login",
"created_at": "2021-08-13 01:29:02"
},
{
"event_attributes": {
"array_within_array": [
{
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
{
"date": "2021-08-18",
"category": "dinner",
"location": "home"
}
]
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
]
}
and I want to convert to the below one
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "meal_selection",
"created_at": "2021-08-13 01:28:57"
}
{
"region": "USA",
"user_id": "123",
"event_name": "login",
"created_at": "2021-08-13 01:29:02"
}
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-18",
"category": "dinner",
"location": "home"
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
Here's a straightforward solution that keeps things simple by using two steps:
{ region, user_id} + (.event_array[] )
| if .event_attributes|has("array_within_array")
then .event_attributes.array_within_array as $a
| .event_attributes = $a[]
else .
end
I have some json from eurostat, which looks like this:
{
"version": "2.0",
"label": "Principaux agrégats des administrations publiques, y compris recettes et dépenses",
"href": "http://ec.europa.eu/eurostat/wdds/rest/data/v2.1/json/fr/gov_10a_main?unit=PC_GDP&na_item=TE§or=S13&time=2008&time=2009&time=2010&time=2011&time=2012&time=2013&time=2014&time=2015&time=2016&time=2017&geo=DE&geo=AT&geo=BE&geo=BG&geo=CY&geo=HR&geo=FI",
"source": "Eurostat",
"updated": "2018-10-26",
"status": {
"57": "b"
},
"extension": {
"datasetId": "gov_10a_main",
"lang": "FR",
"description": null,
"subTitle": null,
"status": {
"label": {
"b": "rupture de série"
}
}
},
"class": "dataset",
"value": {
"0": 49.9,
"1": 54.1,
"2": 52.8,
"3": 50.9,
"4": 51.2,
"5": 51.6,
"6": 52.4,
"7": 51.1,
"8": 50.3,
"9": 49.2,
"10": 50.3,
"11": 54.2,
"12": 53.3,
"13": 54.5,
"14": 55.9,
"15": 55.8,
"16": 55.3,
"17": 53.7,
"18": 53,
"19": 52.2,
"20": 37.1,
"21": 39.4,
"22": 36.2,
"23": 33.8,
"24": 34.5,
"25": 37.7,
"26": 43.1,
"27": 40.5,
"28": 35.1,
"29": 35.1,
"30": 38.4,
"31": 42.1,
"32": 42,
"33": 42.3,
"34": 41.9,
"35": 41.9,
"36": 48.8,
"37": 40.6,
"38": 38,
"39": 37.5,
"40": 43.6,
"41": 47.6,
"42": 47.3,
"43": 44.7,
"44": 44.3,
"45": 44.7,
"46": 44,
"47": 43.7,
"48": 43.9,
"49": 43.9,
"50": 48.3,
"51": 54.8,
"52": 54.8,
"53": 54.4,
"54": 56.2,
"55": 57.5,
"56": 58.1,
"57": 57.1,
"58": 55.9,
"59": 54,
"60": 45.3,
"61": 48.3,
"62": 48,
"63": 48.5,
"64": 47.8,
"65": 47.6,
"66": 48.1,
"67": 48.3,
"68": 46.9,
"69": 45
},
"dimension": {
"unit": {
"label": "unit",
"category": {
"index": {
"PC_GDP": 0
},
"label": {
"PC_GDP": "Pourcentage du produit intérieur brut (PIB)"
}
}
},
"sector": {
"label": "sector",
"category": {
"index": {
"S13": 0
},
"label": {
"S13": "Administrations publiques"
}
}
},
"na_item": {
"label": "na_item",
"category": {
"index": {
"TE": 0
},
"label": {
"TE": "Total des dépenses des administrations publiques"
}
}
},
"geo": {
"label": "geo",
"category": {
"index": {
"AT": 0,
"BE": 1,
"BG": 2,
"CY": 3,
"DE": 4,
"FI": 5,
"HR": 6
},
"label": {
"AT": "Autriche",
"BE": "Belgique",
"BG": "Bulgarie",
"CY": "Chypre",
"DE": "Allemagne (jusqu'en 1990, ancien territoire de la RFA)",
"FI": "Finlande",
"HR": "Croatie"
}
}
},
"time": {
"label": "time",
"category": {
"index": {
"2008": 0,
"2009": 1,
"2010": 2,
"2011": 3,
"2012": 4,
"2013": 5,
"2014": 6,
"2015": 7,
"2016": 8,
"2017": 9
},
"label": {
"2008": "2008",
"2009": "2009",
"2010": "2010",
"2011": "2011",
"2012": "2012",
"2013": "2013",
"2014": "2014",
"2015": "2015",
"2016": "2016",
"2017": "2017"
}
}
}
},
"id": [
"unit",
"sector",
"na_item",
"geo",
"time"
],
"size": [
1,
1,
1,
7,
10
]
}
I would like to produce a csv file.
First, I need to concatenate .status with .value by string (sorry for my poor json knowledge) --> "status":{"57": "b"} with "value":{"57": 57.1}.
Second, I need to produce the same table as the original one (downloaded from eurostat).
I try many jq commands, like:
.status,.value | to_entries
I'm far from finding a solution.
Any help? I think map or map_values/group_by command are needed, but I don't really understand these functions.
EDIT :
I download data from eurostat.
I use their web service here, where I can download data in json format.
I would like to reproduce in shell same table as original, with jq. In my exemple, it should look like :
GEO/TIME,2010,2011,2012,2013,2014,2015,2016,2017
Belgique,"53,3","54,5","55,9","55,8","55,3","53,7","53,0","52,2"
Bulgarie,"36,2","33,8","34,5","37,7","43,1","40,5","35,1","35,1"
"Allemagne (jusqu'en 1990, ancien territoire de la RFA)","47,3","44,7","44,3","44,7","44,0","43,7","43,9","43,9"
Croatie,"48,0","48,5","47,8","47,6","48,1","48,3","46,9","45,0"
Chypre,"42,0","42,3","41,9","41,9","48,8","40,6","38,0","37,5"
Finlande,"54,8","54,4","56,2","57,5","58,1","57,1","55,9","54,0"
But json contain metadata, and Finlande must have 57,1b value.
I hope it's more clear with this edit.
And many thanks for your help.
Your question doesn't indicate very precisely what output you want, but hopefully you'll be able to adapt the following:
.value as $dict
| .status
| to_entries
| map( [.key, .value, $dict[.key]] )
| .[]
| #csv
With your input, and invoking jq with the -r option, this produces:
"57","b",57.1
To show the Paypal plus iFrame (REST API) i make a Request with JSON
{
"intent": "sale",
"experience_profile_id": "XP-XXXX-XXXX-XXX-XXX",
"redirect_urls": {
"return_url": "https://www.XXXXXXX.de/bestellen.php",
"cancel_url": "https://www.XXXXXXX.de/zahlungabbruch.php"
},
"payer": {
"payment_method": "paypal"
},
"transactions": [{
"amount": {
"total": "53.45",
"currency": "EUR",
"details": {
"subtotal": "49.5",
"shipping": "3.95"
}
},
"description": "Tollewolle",
"invoice_number": "",
"item_list": {
"items": [
{
"quantity": "4",
"name": "Fine Kid - 50",
"price": "8.25",
"currency": "EUR",
"sku": "8-50"
},
{
"quantity": "2",
"name": "Fine Kid - 208",
"price": "8.25",
"currency": "EUR",
"sku": "8-208"
}
]
},
"shipping_address": {
"line1": "Rechnungs Str. 41",
"city": "Flensburg",
"postal_code": "24939",
"country_code": "DE"
}
}]
}
Without the shipping_address it works fine.
With the address i get an Error 'MALFORMED_REQUEST'
I believe Shipping address is a child ob lineitems so you need to move it up a level. E.g.
"transactions": [{"amount": {"total": "53.45", "currency":
"EUR","details":{"subtotal": "49.5", "shipping":
"3.95"}},"description": "Tollewolle", "invoice_number": "",
"item_list": {"items": [{"quantity": "4", "name": "Fine Kid - 50",
"price": "8.25", "currency": "EUR", "sku": "8-50"},{"quantity": "2",
"name": "Fine Kid - 208","price": "8.25", "currency": "EUR", "sku":
"8-208"}],"shipping_address": {"line1": "Rechnungs Str. 41","city":
"Flensburg", "postal_code": "24939", "country_code": "DE"}}}]
I have a great problem for parse a very difficult json string. For Eg
{
"facilityDetails": [
{
"tableName": "FACILITY",
"facilityDetails": [
{
"id": 1,
"itemId": "s101",
"name": "facility",
"status": 1,
"lastEditedOn": "01/Jan/201200: 00: 00.000"
}
]
},
{
"tableName": "PLACE_SERVICE",
"facilityDetails": [
{
"id": 1,
"itemId": "22",
"name": "placeservice",
"facility": "5",
"status": 1,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 2,
"itemId": "55",
"name": "placeservice",
"facility": "t",
"status": 2,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 3,
"itemId": "99",
"name": "placeservice",
"facility": "r",
"status": 33,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
},
{
"id": 4,
"itemId": "22",
"name": "placeservice",
"facility": "",
"status": 0,
"lastEditedOn": "01/Jan/201000: 00: 00.000"
}
]
},
{
"tableName": "AGENT",
"facilityDetails": [
{
"agentId": 2,
"itemId": "1",
"name": "agent",
"defUnitId": 0,
"defRouteId": 0,
"color": "",
"synonyms": "",
"administrationType": 0,
"status": 0,
"lastEditedOn": "01/Jan/201200: 00: 00.000"
}
]
}
]
}
for this json string "facilityDetails" is an inner array that have different details at each time. How can i parse this type of json. If anyone know please help me
You should be able to parse this with the Json.NET library.
http://james.newtonking.com/projects/json-net.aspx