I have hapi.js, sequelize with mysql with script like this :
method: 'GET',
path: `/${GROUP_NAME}`,
options: {
tags: ['api', GROUP_NAME],
description: 'Mendapatkan jumlah tempat tidur berdasarkan kelas',
notes: 'Mendapatkan jumlah tempat tidur',
handler: async (request, h) => {
return jlhttidurbyjenis.findAll({ attributes: ['VIP','KELAS 1','KELAS 2','KELAS 3','ICU','NICU','PICU','HCU','ICCU','ISOLASI']})
},
validate: {
},
response: {
}
}
when I test with postman it response like this :
[
{
"VIP": {
"type": "Buffer",
"data": [
50,
47,
50,
50
]
},
"KELAS 1": {
"type": "Buffer",
"data": [
48,
47,
48
]
},
"KELAS 2": {
"type": "Buffer",
"data": [
48,
47,
48
]
},
"KELAS 3": {
"type": "Buffer",
"data": [
48,
47,
48
]
},
"ICU": {
"type": "Buffer",
"data": [
48,
47,
48
]
},
"NICU": {
"type": "Buffer",
"data": [
48,
47,
48
]
},
"PICU": {
"type": "Buffer",
"data": [
48,
47,
48
]
},
"HCU": {
"type": "Buffer",
"data": [
48,
47,
48
]
},
"ICCU": {
"type": "Buffer",
"data": [
48,
47,
48
]
},
"ISOLASI": {
"type": "Buffer",
"data": [
48,
47,
48
]
}
}
]
How to fix the script so the response will be the same with database content, it will be like this :
[
{
"VIP": "12/22",
"KELAS 1": "0/0",
"KELAS 2": "0/0",
"KELAS 3": "0/0",
"ICU": "0/0",
"NICU": "0/0",
"PICU": "0/0",
"HCU": "0/0",
"ICCU": "0/0",
"ISOLASI": "0/0"
}
]
That looks like a serialization issue from sequilize. Look at available configurations for JSON serialization with regards to buffer.
Related
I would like to translate the field "text" from the flight domain of the Taskmaster-2 dataset. Which is a deeply nested JSON file. Using Google Cloud Translate how can I do it?
Example (from English to Bangla):
Origin JSON file:
[ {
"conversation_id": "dlg-00100680-00e0-40fe-8321-6d81b21bfc4f",
"instruction_id": "flight-12",
"utterances": [
{
"index": 0,
"speaker": "USER",
"text": "Hello. I'd like to find a round trip commercial airline flight from San Francisco to Denver.",
"segments": [
{
"start_index": 26,
"end_index": 36,
"text": "round trip",
"annotations": [
{
"name": "flight_search.type"
}
]
},
Output JSON file:
[ {
"conversation_id": "dlg-00100680-00e0-40fe-8321-6d81b21bfc4f",
"instruction_id": "flight-12",
"utterances": [
{
"index": 0,
"speaker": "USER",
"text": "হ্যালো. আমি সান ফ্রান্সিসকো থেকে ডেনভার পর্যন্ত একটি রাউন্ড ট্রিপ বাণিজ্যিক এয়ারলাইন ফ্লাইট খুঁজতে চাই।",
"segments": [
{
"start_index": 26,
"end_index": 36,
"text": "রাউন্ড ট্রিপ",
"annotations": [
{
"name": "flight_search.type"
}
]
},
I extracted a few lines of data in flights.json and used the code below written in Python using Google Cloud Translation API to translate English to Japanese. Also see list of supported languages of the API.
test.json:
[
{
"conversation_id": "dlg-00100680-00e0-40fe-8321-6d81b21bfc4f",
"instruction_id": "flight-12",
"utterances": [
{
"index": 0,
"speaker": "USER",
"text": "Hello. I'd like to find a round trip commercial airline flight from San Francisco to Denver.",
"segments": [
{
"start_index": 26,
"end_index": 36,
"text": "round trip",
"annotations": [
{
"name": "flight_search.type"
}
]
},
{
"start_index": 68,
"end_index": 81,
"text": "San Francisco",
"annotations": [
{
"name": "flight_search.origin"
}
]
},
{
"start_index": 85,
"end_index": 91,
"text": "Denver",
"annotations": [
{
"name": "flight_search.destination1"
}
]
}
]
},
{
"index": 1,
"speaker": "ASSISTANT",
"text": "Hello, how can I help you?"
},
{
"index": 2,
"speaker": "ASSISTANT",
"text": "San Francisco to Denver, got it.",
"segments": [
{
"start_index": 0,
"end_index": 13,
"text": "San Francisco",
"annotations": [
{
"name": "flight_search.origin"
}
]
},
{
"start_index": 17,
"end_index": 23,
"text": "Denver",
"annotations": [
{
"name": "flight_search.destination1"
}
]
}
]
}
]
}
]
Code:
import json
from google.cloud import translate_v2 as translate
f = open('test.json')
data = json.load(f)
target = "ja"
translate_client = translate.Client()
for conv in data:
for utt in conv["utterances"]:
utt["text"] = translate_client.translate(utt["text"], target_language=target)["translatedText"]
if "segments" in utt:
for seg in utt["segments"]:
seg["text"] = translate_client.translate(seg["text"], target_language=target)["translatedText"]
#print(data) # prints a dictionary
json_object = json.dumps(data, indent=2,ensure_ascii=False).encode('utf8')
print(json_object.decode()) # prints a json string
Output:
[
{
"conversation_id": "dlg-00100680-00e0-40fe-8321-6d81b21bfc4f",
"instruction_id": "flight-12",
"utterances": [
{
"index": 0,
"speaker": "USER",
"text": "こんにちは。サンフランシスコからデンバーまでの民間航空会社の往復便を探したいのですが。",
"segments": [
{
"start_index": 26,
"end_index": 36,
"text": "往復",
"annotations": [
{
"name": "flight_search.type"
}
]
},
{
"start_index": 68,
"end_index": 81,
"text": "サンフランシスコ",
"annotations": [
{
"name": "flight_search.origin"
}
]
},
{
"start_index": 85,
"end_index": 91,
"text": "デンバー",
"annotations": [
{
"name": "flight_search.destination1"
}
]
}
]
},
{ "index": 1,
"speaker": "ASSISTANT",
"text": "こんにちは、どうすればいいですか?"
},
{
"index": 2,
"speaker": "ASSISTANT",
"text": "サンフランシスコからデンバーへ、了解。",
"segments": [
{
"start_index": 0,
"end_index": 13,
"text": "サンフランシスコ",
"annotations": [
{
"name": "flight_search.origin"
}
]
},
{
"start_index": 17,
"end_index": 23,
"text": "デンバー",
"annotations": [
{
"name": "flight_search.destination1"
}
]
}
]
}
]
}
]
I've taken the following JSON example directly from Twitter's API example using Postman, my question is how would I be able to grab each tweets inside the "data" section and add each individual tweet onto a List < String > so that each individual tweet's sub JSON is saved as String in the List. Would this be possible? I was attempting to use the JSON parsing method decoding and encoding from Dart but that did not work, I'm not sure if its because the JSON example features the "data" section and the "errors" section. Any help would be greatly appreciated, thank you!
Example JSON:
{
"data": [
{
"author_id": "12",
"conversation_id": "20",
"created_at": "2006-03-21T20:50:14.000Z",
"id": "20",
"text": "just setting up my twttr"
},
{
"attachments": {
"media_keys": [
"16_1276500934466703361"
]
},
"author_id": "783214",
"conversation_id": "1275244210439028736",
"created_at": "2020-06-23T01:48:07.000Z",
"entities": {
"urls": [
{
"start": 112,
"end": 135,
"url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
"display_url": "pic.twitter.com/dpI2lRmj9F"
}
]
},
"id": "1275244210439028736",
"text": "Need to follow what’s happening in real time? Change your timeline to show latest Tweets instead of top Tweets."
},
{
"attachments": {
"media_keys": [
"3_1274087263073255425"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:32.000Z",
"entities": {
"mentions": [
{
"start": 13,
"end": 22,
"username": "YoliZama"
}
],
"urls": [
{
"start": 23,
"end": 46,
"url": "https://twitter.com/Twitter/status/1274087695145332736/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087695145332736/photo/1",
"display_url": "pic.twitter.com/lcGDLzAJIn"
}
]
},
"id": "1274087695145332736",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087694105075714"
}
],
"text": "📍 Oakland\n🗣️ #YoliZama"
},
{
"attachments": {
"media_keys": [
"3_1274086977952833536"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:32.000Z",
"entities": {
"mentions": [
{
"start": 19,
"end": 31,
"username": "Afrikkana95"
}
],
"urls": [
{
"start": 32,
"end": 55,
"url": "https://twitter.com/Twitter/status/1274087694105075714/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087694105075714/photo/1",
"display_url": "pic.twitter.com/tEfs27p7xu"
}
]
},
"id": "1274087694105075714",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087692003770368"
}
],
"text": "📍 New York City\n🗣️ #Afrikkana95"
},
{
"attachments": {
"media_keys": [
"3_1274086862907305984"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:31.000Z",
"entities": {
"mentions": [
{
"start": 13,
"end": 25,
"username": "JoshuaKissi"
}
],
"urls": [
{
"start": 26,
"end": 49,
"url": "https://twitter.com/Twitter/status/1274087692003770368/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087692003770368/photo/1",
"display_url": "pic.twitter.com/ZeD3XvJUbX"
}
]
},
"id": "1274087692003770368",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087690758090752"
}
],
"text": "📍 Chicago\n🗣️ #JoshuaKissi"
},
{
"attachments": {
"media_keys": [
"3_1274086703272038401"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:31.000Z",
"entities": {
"mentions": [
{
"start": 18,
"end": 33,
"username": "Imani_Barbarin"
}
],
"urls": [
{
"start": 34,
"end": 57,
"url": "https://twitter.com/Twitter/status/1274087690758090752/photo/1",
"display_url": "pic.twitter.com/ZRDUipsu38",
"expanded_url": "https://twitter.com/Twitter/status/1274087690758090752/photo/1",
"display_url": "pic.twitter.com/ZRDUipsu38"
}
]
},
"id": "1274087690758090752",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087689487134720"
}
],
"text": "📍 Philadelphia\n🗣️ #Imani_Barbarin "
},
{
"attachments": {
"media_keys": [
"3_1274086530919718917"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:30.000Z",
"entities": {
"mentions": [
{
"start": 13,
"end": 25,
"username": "BerniceKing"
}
],
"urls": [
{
"start": 26,
"end": 49,
"url": "https://twitter.com/Twitter/status/1274087688321200128/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087688321200128/photo/1",
"display_url": "pic.twitter.com/83upyVnwIS"
}
]
},
"id": "1274087688321200128",
"referenced_tweets": [
{
"type": "replied_to",
"id": "1274087687469715457"
}
],
"text": "📍 Atlanta\n🗣️ #BerniceKing "
},
{
"attachments": {
"media_keys": [
"3_1274086027544498176"
]
},
"author_id": "783214",
"conversation_id": "1274087687469715457",
"created_at": "2020-06-19T21:12:30.000Z",
"entities": {
"mentions": [
{
"start": 17,
"end": 29,
"username": "FredTJoseph"
}
],
"urls": [
{
"start": 30,
"end": 53,
"url": "https://twitter.com/Twitter/status/1274087687469715457/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1274087687469715457/photo/1",
"display_url": "pic.twitter.com/lNTOkyguG1"
}
]
},
"id": "1274087687469715457",
"text": "📍 Minneapolis\n🗣️ #FredTJoseph"
},
{
"author_id": "783214",
"conversation_id": "1274034244700930049",
"created_at": "2020-06-19T17:40:09.000Z",
"entities": {
"hashtags": [
{
"start": 106,
"end": 115,
"tag": "BlackJoy"
}
],
"mentions": [
{
"start": 3,
"end": 14,
"username": "Blackbirds"
}
]
},
"id": "1274034244700930049",
"referenced_tweets": [
{
"type": "retweeted",
"id": "1274014870707437570"
}
],
"text": "RT #Blackbirds: Juneteenth is a celebration. It’s about our freedom. And within that freedom is our joy.\n\n#BlackJoy is a form of resistance…"
},
{
"author_id": "773578328498372608",
"conversation_id": "1275473478779469825",
"created_at": "2020-06-23T16:59:09.000Z",
"entities": {
"mentions": [
{
"start": 3,
"end": 10,
"username": "Policy"
}
]
},
"id": "1275473478779469825",
"referenced_tweets": [
{
"type": "retweeted",
"id": "1275192966953476100"
}
],
"text": "RT #Policy: Statement on US high-skilled immigration proclamation: \n\n\"This proclamation undermines America’s greatest economic asset: its d…"
}
],
"errors": [
{
"detail": "Could not find tweet with ids: [1276230436478386177].",
"title": "Not Found Error",
"resource_type": "tweet",
"parameter": "ids",
"value": "1276230436478386177",
"type": "https://api.twitter.com/2/problems/resource-not-found"
}
]
}
Output: List < String > where one element in the list could look like this:
{
"attachments": {
"media_keys": [
"16_1276500934466703361"
]
},
"author_id": "783214",
"conversation_id": "1275244210439028736",
"created_at": "2020-06-23T01:48:07.000Z",
"entities": {
"urls": [
{
"start": 112,
"end": 135,
"url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
"expanded_url": "https://twitter.com/Twitter/status/1275244210439028736/photo/1",
"display_url": "pic.twitter.com/dpI2lRmj9F"
}
]
},
"id": "1275244210439028736",
"text": "Need to follow what’s happening in real time? Change your timeline to show latest Tweets instead of top Tweets."
},
It would essentially grab one whole tweet's subJson and save it onto the List. This goes hand in hand with an issue I was facing a few days back concerning this problem , but I think the problem is that the JSON response I get back using the HTTP library is not able to be added properly onto a List.
List<Map<String, dynamic>> jsonRes =
(inputMap['data'] as List<Map>).map((t) => {
if (t.containsKey('attachments')) 'attachments': t['attachments'],
if (t.containsKey('author_id')) 'author_id': t['author_id'],
if (t.containsKey('conversation_id')) 'conversation_id': t['conversation_id'],
if (t.containsKey('created_at')) 'created_at': t['created_at'],
if (t.containsKey('entities')) 'entities': t['entities'],
if (t.containsKey('id')) 'id': t['id'],
if (t.containsKey('text')) 'text': t['text'],
}).toList();
List<String> stringRes = jsonRes.map((e) => jsonEncode(e)).toList();
Alternatively:
final List<String> wantedKeys = [
'attachments',
'author_id',
'conversation_id',
'created_at',
'entities',
'id',
'text'
];
List<Map<String, dynamic>> jsonRes = (inputMap['data']
as List<Map<String, dynamic>>)
.map((m) => Map.fromEntries(m.entries.where((e) => wantedKeys.contains(e.key))))
.toList();
List<String> stringRes = jsonRes.map((e) => jsonEncode(e)).toList();
Hellow jq experts!
I'm a jq learner and have a json obect composed lists as follows:
{
"image_files": [
{
"id": "img_0001",
"width": 32,
"heigt": 32,
"file_name": "img_0001.png"
},
{
"id": "img_0002",
"width": 128,
"heigt": 32,
"file_name": "img_0002.png"
},
{
"id": "img_0003",
"width": 32,
"heigt": 32,
"file_name": "img_0003.png"
},
{
"id": "img_0004",
"width": 160,
"heigt": 32,
"file_name": "img_0004.png"
}
],
"annotations": [
{
"id": "ann_0001",
"image_id": "img_0001",
"label": "A",
"attributes": {
"type": "letter",
"augmented": false
}
},
{
"id": "ann_0002",
"image_id": "img_0002",
"label": "Good",
"attributes": {
"type": "word",
"augmented": false
}
},
{
"id": "ann_0003",
"image_id": "img_0003",
"label": "C",
"attributes": {
"type": "letter",
"augmented": false
}
},
{
"id": "ann_0004",
"image_id": "img_0004",
"label": "Hello",
"attributes": {
"type": "word",
"augmented": false
}
}
]
}
image_id in the annotations list are foreign key referencing the id in the image_files list.
I want to join image_files and annotations with condition of annotations.attribute.type == "letter".
Expecting following ouptut:
{
"letter_image_files_with_label": [
{
"id": "img_0001",
"width": 32,
"heigt": 32,
"file_name": "img_0001.png",
"label": "A"
},
{
"id": "img_0003",
"width": 32,
"heigt": 32,
"file_name": "img_0003.png",
"label": "C"
}
]
}
How can I produce above result from the json data input?
join explained in jq manual does not seem to use this kind task.
Is there a way for this? Please show me the rope.
Thanks for your generous reading.
Indexing image_files with ids makes this pretty trivial.
INDEX(.image_files[]; .id) as $imgs | [
.annotations[]
| select(.attributes.type == "letter")
| $imgs[.image_id] + {label: .label}
]
Online demo
I built my Conversational AI chatbot and followed all the steps to connect it to Alexa but when I try to call my chatbot in alexa simulator,I get the following message:
"There was a problem with the requested skill's response"
this is the code which appears on JSON output 1 and JSON output 2 after the error:
{
"version": "1.0",
"session": {
"new": true,
"sessionId": "amzn1.echo-api.session.5e3bb3da-ba51-4422-80cb-c5cace7e3756",
"application": {
"applicationId": "amzn1.ask.skill.bfb81ac5-d463-4201-b70c-a35f8cc6dbed"
},
"user": {
"userId": "amzn1.ask.account.AHIAWE37M4SE4Q7DBFIIRLYZYXFKPU42HAD35P5XLHMZA4RGRXFERFRKPAN2I34A56TCC2XEGVNKYH7SW2KVB2NBUYAAY73GRDDXNEFFFY46W2FJXXCLSMAIUWDEUESU3ZMGIYPPYA2WXFXBBHGJBLHNVRYBLXTBLOCJMGOMK4VXNONXGGICYKBBIE7LMCTSUOVEYRAS3E2ATNQ"
}
},
"context": {
"System": {
"application": {
"applicationId": "amzn1.ask.skill.bfb81ac5-d463-4201-b70c-a35f8cc6dbed"
},
"user": {
"userId": "amzn1.ask.account.AHIAWE37M4SE4Q7DBFIIRLYZYXFKPU42HAD35P5XLHMZA4RGRXFERFRKPAN2I34A56TCC2XEGVNKYH7SW2KVB2NBUYAAY73GRDDXNEFFFY46W2FJXXCLSMAIUWDEUESU3ZMGIYPPYA2WXFXBBHGJBLHNVRYBLXTBLOCJMGOMK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1xtyBtkafcOFQPN3UAzFKPI85jUtIFuYUsuIbl7N_f64yOAgtkIQEvjCA-DWQzkerPyHJo2VrF0q7Tw"
},
"Viewport": {
"experiences": [
{
"arcMinuteWidth": 246,
"arcMinuteHeight": 144,
"canRotate": false,
"canResize": false
}
],
"shape": "RECTANGLE",
"pixelWidth": 1024,
"pixelHeight": 600,
"dpi": 160,
"currentPixelWidth": 1024,
"currentPixelHeight": 600,
"touch": [
"SINGLE"
],
"video": {
"codecs": [
"H_264_42",
"H_264_41"
]
}
},
"Viewports": [
{
"type": "APL",
"id": "main",
"shape": "RECTANGLE",
"dpi": 160,
"presentationType": "STANDARD",
"canRotate": false,
"configuration": {
"current": {
"video": {
"codecs": [
"H_264_42",
"H_264_41"
]
},
"size": {
"type": "DISCRETE",
"pixelWidth": 1024,
"pixelHeight": 600
}
}
}
}
]
},
"request": {
"type": "LaunchRequest",
"requestId": "amzn1.echo-api.request.1d812f0b-5bce-42a6-bdd5-4791d5876e8e",
"timestamp": "2020-01-22T09:20:12Z",
"locale": "en-US",
"shouldLinkResultBeReturned": false
}}
JSON Output 2:
{
"version": "1.0",
"session": {
"new": false,
"sessionId": "amzn1.echo-api.session.5e3bb3da-ba51-4422-80cb-c5cace7e3756",
"application": {
"applicationId": "amzn1.ask.skill.bfb81ac5-d463-4201-b70c-a35f8cc6dbed"
},
"user": {
"userId": "amzn1.ask.account.AHIAWE37M4SE4Q7DBFIIRLYZYXFKPU42HAD35P5XLHMZA4RGRXFERFRKPAN2I34A56TCC2XEGVNKYH7SW2KVB2NBUYAAY73GRDDXNEFFFY46W2FJXXCLSMAIUWDEUESU3ZMGIYPPYA2WXFXBBHGJBLHNVRYBLXTBLOCJMGOMK4VXNONXGGICYKBBIE7LMCTSUOVEYRAS3E2ATNQ"
}
},
"context": {
"System": {
"application": {
"applicationId": "amzn1.ask.skill.bfb81ac5-d463-4201-b70c-a35f8cc6dbed"
},
"user": {
"userId": "amzn1.ask.account.AHIAWE37M4SE4Q7DBFI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1iXP8qhiPQeu8g7Xwl2XVpFUAuaeqoSyZxzM2dnKBIND18qrcDfDTS9YRzwcrQdBaGlH-_uW0nXDOntyZ7myqB50fBVtlz5xiayHFE5J3-OTaMK4nuggMKzuL8lbYQrcEPwwbt940LiKy4CRjltHA83N1xtyBtkafcOFQPN3UAzFKPI85jUtIFuYUsuIbl7N_f64yOAgtkIQEvjCA-DWQzkerPyHJo2VrF0q7Tw"
},
"Viewport": {
"experiences": [
{
"arcMinuteWidth": 246,
"arcMinuteHeight": 144,
"canRotate": false,
"canResize": false
}
],
"shape": "RECTANGLE",
"pixelWidth": 1024,
"pixelHeight": 600,
"dpi": 160,
"currentPixelWidth": 1024,
"currentPixelHeight": 600,
"touch": [
"SINGLE"
],
"video": {
"codecs": [
"H_264_42",
"H_264_41"
]
}
},
"Viewports": [
{
"type": "APL",
"id": "main",
"shape": "RECTANGLE",
"dpi": 160,
"presentationType": "STANDARD",
"canRotate": false,
"configuration": {
"current": {
"video": {
"codecs": [
"H_264_42",
"H_264_41"
]
},
"size": {
"type": "DISCRETE",
"pixelWidth": 1024,
"pixelHeight": 600
}
}
}
}
]
},
"request": {
"type": "SessionEndedRequest",
"requestId": "amzn1.echo-api.request.30c39df8-dc26-4dcf-a193-de432a3a0ab1",
"timestamp": "2020-01-22T09:20:13Z",
"locale": "en-US",
"reason": "ERROR",
"error": {
"type": "INVALID_RESPONSE",
"message": "An exception occurred while dispatching the request to the skill."
}
}}{
"body": {
"version": "1.0",
"response": {
"type": "_DEFAULT_RESPONSE"
},
"sessionAttributes": {},
"userAgent": "ask-node/2.7.0 Node/v8.15.0"
}
}
Is it possible to modify/replace array elements while grouping by a specific key (.[].Parameter.Id) such that this array:
[{
"Id": 48,
"Parameter": {
"Id": 17
}
}, {
"Id": 196,
"Parameter": {
"Id": 17
}
}]
becomes this:
[
{
"p17": [48, 196]
}
]
Here is the source JSON file for a complete example:
[{
"Id": 78,
"PromotionType": 2,
"Amount": "100",
"UpperLimit": null,
"Variables": [{
"Id": 100,
"Parameter": {
"Id": 30
}
}]
}, {
"Id": 84,
"PromotionType": 2,
"Amount": null,
"UpperLimit": null,
"Variables": [{
"Id": 48,
"Parameter": {
"Id": 17
}
}, {
"Id": 196,
"Parameter": {
"Id": 17
}
}, {
"Id": 59,
"Parameter": {
"Id": 21
}
}, {
"Id": 60,
"Parameter": {
"Id": 21
}
}, {
"Id": 62,
"Parameter": {
"Id": 21
}
}]
}, {
"Id": 59,
"PromotionType": 2,
"Amount": "666.6",
"UpperLimit": null,
"Variables": [{
"Id": 96,
"Parameter": {
"Id": 8
}
}, {
"Id": 47,
"Parameter": {
"Id": 17
}
}]
}]
What I want to achieve is this:
[{
"Id": 78,
"PromotionType": 2,
"Amount": "100",
"UpperLimit": null,
"Variables": [{
"p30": [100]
}]
}, {
"Id": 84,
"PromotionType": 2,
"Amount": null,
"UpperLimit": null,
"Variables": [{
"p17": [48, 196]
}, {
"p21": [59, 60, 62]
}]
}, {
"Id": 59,
"PromotionType": 2,
"Amount": "666.6",
"UpperLimit": null,
"Variables": [{
"p8": [96]
}, {
"p17": [47]
}]
}]
I am reading through jq manual, jq cookbook and found some functions (e.g. with_entries, unique_by, inputs) that might help but could not figure out how to make it work.
Number of objects/inner objects are also not fixed. So I cannot simply replace using array indexes.
Any help would be appreciated.
Thanks,
Emre
jq solution:
jq 'map(.Variables
|= (group_by(.Parameter.Id)
| map(("p" + (.[0].Parameter.Id | tostring)) as $pid
| { ($pid) : map(.Id) }
)
)
)' input.json
The output:
[
{
"Id": 78,
"PromotionType": 2,
"Amount": "100",
"UpperLimit": null,
"Variables": [
{
"p30": [
100
]
}
]
},
{
"Id": 84,
"PromotionType": 2,
"Amount": null,
"UpperLimit": null,
"Variables": [
{
"p17": [
48,
196
]
},
{
"p21": [
59,
60,
62
]
}
]
},
{
"Id": 59,
"PromotionType": 2,
"Amount": "666.6",
"UpperLimit": null,
"Variables": [
{
"p8": [
96
]
},
{
"p17": [
47
]
}
]
}
]