JMESPath export keys based on value from JSON object - json

I'm trying to filter simple JSON data and export keys with similar values using the JMESPath query. This is my JSON.
{
"af": "0",
"al": "2",
"dz": "0",
"ad": "2",
"ao": "1",
"ag": "2",
"ar": "2",
"am": "2",
"au": "3",
"at": "2"
}
Desired output would be this if I specify "0"
{
"af",
"dz"
}
My current query is keys(#)[?contains(#, '0')] but it returns empty array.

Related

csv to json or json format as column array command line

is it possible to obtain this output ?
With a command line tool (jq or other)
From csv
id,age,gender
1,39,M
2,25,M
To json column array
[{"id":["1","2"]},{"age":["39","25"]},{"gender":["M","M"]}]
OR from json
[
{
"id": "1",
"age": "39",
"gender": "M"
},
{
"id": "2",
"age": "25",
"gender": "M"
}
]
To json column array
[{"id":["1","2"]},{"age":["39","25"]},{"gender":["M","M"]}]
Hoping you understood me. Thank you in advance for your answers.
There is no cannonical way to read in CSV as the format is not standardized. Existing usages mostly differ in the field separator and the escape sequence (used if either one happens to be part of the data), which you would have to consider explicitly when reading in raw text manually.
Therefore, I regard it as safer to follow your second approach. Here's one way for the given input:
[
{
"id": "1",
"age": "39",
"gender": "M"
},
{
"id": "2",
"age": "25",
"gender": "M"
}
]
jq 'map(to_entries) | transpose | map({(first.key): map(.value)}) | add'
{
"id": [
"1",
"2"
],
"age": [
"39",
"25"
],
"gender": [
"M",
"M"
]
}
Demo

Convert Nested JSON to Flat JSON using jolt Spec based on multiple inputs (if else)

I need to convert the nested json to flat json by given type of input, if its flat json or nested json gives output as flat json respectively. I haven't seen any correct resource related to the this topic. I given the inputs and outputs below.
I am having some trouble with converting the nested JSON to flat JSON, and didn't get any closer as to what is mentioned problem. I need to transform a JSON structure by using a JOLT spec. I use https://jolt-demo.appspot.com to test the following below. Help me, how can i get the expected out, with this code
Input-1:
{
"MessageType": "CREATION",
"Number": "123",
"Status": "created sucessfully",
"StopSequence": "1",
"Code": [
{
"CodeName": "ABC",
"ShortDescription": "short description about ABC",
"TimeImpact": 234,
"Rank": 1
},
{
"ReasonCodeName": "XYZ",
"ShortDescription": "short description about ABC",
"TimeImpact": 123,
"Rank": 2
}
]
}
Input-2:
{
"MessageType": "UPDATE",
"Number": "345",
"PNumber": "P123",
"Status": "updated sucessfully",
"StopSequence": "2",
"Id": 1234,
"LNumber": "34565",
"DeviceID": "7645235",
"Timestamp": "2015-10-01T16:00:00-05:00",
"Timezone": "US/New_York",
"TimezoneShortName": "EST",
"Unlocode": "XXXX",
}
Expected Output-1 if input 1 passes:
[
{
"MessageType": "CREATION",
"Number": "123",
"Status": "created sucessfully",
"StopSequence": "1",
"Code": "1",
"CodeName": "ABC",
"ShortDescription": "short description about ABC",
"TimeImpact": 234,
"CodeRank": 1
},
{
"MessageType": "CREATION",
"Number": "123",
"Status": "created sucessfully",
"StopSequence": "1",
"Code": "2",
"ReasonCodeName": "XYZ",
"ShortDescription": "short description about ABC",
"TimeImpact": 123,
"Rank": 2
}
]
Expected Output-2 if input 2 passes:
{
"MessageType": "UPDATE",
"PNumber": "P123",
"Status": "updated sucessfully",
"StopSequence": "2",
"Id": 1234,
"Timestamp": "2015-10-01T16:00:00-05:00",
"Timezone": "US/New_York",
}
What you need is to repeat as much as the number of objects of the Code array. So, walk through by them as picking the related values from the out of the array such as
[
{
"operation": "shift",
"spec": {
"Code": {
"*": {
"#(2,MessageType)": "[&1].MessageType", // "#(2,MessageType)" means going up the tree 2 levels to reach the position of "MessageType" , [&1] means combining the values as array of objects depending on the indexes of the array
"#(2,Number)": "[&1].Number",
"#(2,Status)": "[&1].Status",
"#(2,StopSequence)": "[&1].StopSequence",
"#(0,Rank)": "[&1].Code", // to copy the values of the "Rank" to "Code"
"*": "[&1].&"
}
},
"MessageT*|PNu*|St*|Id|Times*|Timezone": "&"
}
}
]

Find the value of key from JSON with Shell Script without using jq

I am looking to read a JSON file using shell script and getting Id based on the name attribute and store it in a variable without using "jq". Please suggest how it can be done. The json looks like:
{
"elements": [
{
"id": "1",
"internalId": "AA",
"name": "SampleService",
},
{
"id": "2",
"internalId": "BB",
"name": "Loan_Evaluation",
},
{
"id": "3",
"internalId": "CC",
"name": "Miniloan Service",
}
],`
}

How to display mysql nested json data in multiple columns?

How to parse nested JSON data in MySQL? Anyone Kindly help me with that.
Sample JSON Data.
[
{
"code": "01",
"exits": "0",
"enters": "0",
"start_time": "00:00:00"
},
{
"code": "01",
"exits": "0",
"enters": "0",
"start_time": "00:15:00"
},
{
"code": "01",
"exits": "0",
"enters": "0",
"start_time": "00:30:00"
}
]
I want to be displayed like.
ID code-start time code-start time
1 01-00:00:00 01-00:15:00 .....etc upto 96 columns
database Table structure:
id auto_increment
json_data json

Get Li3 to return JSON results as an array of objects, not an object of objects

I am trying to utilize the JSON result of a GET request to my Li3 app, but I would like the result to be an array of the returned JSON objects, rather than an object of the JSON objects.
I have the following code in my view file (index.html.php):
print($todos->to('json'));
Which results in each row becoming a JSON object (good), but within an over-arching JSON object.
{
"1": {
"id": "1",
"title": "One",
"done": "0"
},
"2": {
"id": "2",
"title": "Two",
"done": "0"
},
"3": {
"id": "3",
"title": "Three",
"done": "0"
},
"4": {
"id": "4",
"title": "Four",
"done": "0"
}
}
I would like to get:
[
{
"id": "1",
"title": "One",
"done": "0"
},
{
"id": "2",
"title": "Two",
"done": "0"
},
{
"id": "3",
"title": "Three",
"done": "0"
},
{
"id": "4",
"title": "Four",
"done": "0"
}
]
Note: I've found that this was the case (array of objects) in commit "974469cf25db5cbab61f3e1ff172405f4635032e" of the lithium github project, but with anything after that commit, the result is an object of objects.
Try $todos->to('json', ['indexed' => false]), or, refer to the Media class for direct serialization of JSON without the template.
Todos::all(['return' => 'array'))->to('json'); works perfect with RecordSet too