Groovy script for escaping double quotes in JSON - json

I have a requirement wherein the output JSON field needs to have an escaped double quotes ("). I managed to have the quotes escaped in the field values by hardcoding the quotes. But now, I'm not so sure on how to put a backslash in the start and end of the field.
Please see sample below:
{ "Request":
[ {
"content": { "row": [
{ "fieldA": [ "tax\":\"US\",\"percent\": 6.250000000,\"value\": 10000.000000000,\"description\":\"TaxUS\",\"currency\":\"USD" ]},
{"fieldA": [ "tax\":\"\",\"percent\": 0.000000000,\"value\": 170000.000000000,\"description\":\"Total\",\"currency\":\"USD" ] },
{ "fieldB": [ "item\":\"000000\",\"Line\":0000,\"Date\":\"20201207\",\"Qty\": 2000,\"confirm\": 0.00" ] },
{"fieldB": [ "item\":\"000001\",\"Line\":0001,\"Date\":\"20201208\",\"Qty\": 1000,\"confirm\": 2000.00" ] } ] } }
I need to have a backslash at the beginning and the end of the fieldA and fieldB arrays.
This is the expected output:
{ "Request":
[ {
"content": { "row": [
{ "fieldA": [ \"tax\":\"US\",\"percent\": 6.250000000,\"value\": 10000.000000000,\"description\":\"TaxUS\",\"currency\":\"USD\" ]},
{"fieldA": [ \"tax\":\"\",\"percent\": 0.000000000,\"value\": 170000.000000000,\"description\":\"Total\",\"currency\":\"USD\" ] },
{ "fieldB": [ \"item\":\"000000\",\"Line\":0000,\"Date\":\"20201207\",\"Qty\": 2000,\"confirm\": 0.00\" ] },
{"fieldB": [ \"item\":\"000001\",\"Line\":0001,\"Date\":\"20201208\",\"Qty\": 1000,\"confirm\": 2000.00\" ] } ] } }
I would like to ask for tips in adding the backslash for the requirement.
Thanks everyone!

Related

JQ get data from an array of array

How can I get a value from an array of arrays. This is JSON from which I have to get the data:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"myMetric": "ABC"
},
"values": [
[
1633597734,
"64.54166666666667"
],
[
1633598034,
"65.51666666666667" <-- wanted value
]
]
}
]
}
}
I have to get value from the last array in values.
I tried the following:
jq .data.result.values input.json
jq .data.result.values[] input.json
jq .data.result.values[][] input.json
For every one the result is:
jq: error (at json:0): Cannot index array with string "values"
How can I get value from the last array in values?
You can use a negative index to enumerate from the back of an array: to get the last element of the last array in the values of the last element of result:
.data.result[-1].values[-1][-1]
demo: https://jqplay.org/s/YMpB8-A4-Q
filter:
.data.result[].values[1][1]?
input:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"myMetric": "ABC"
},
"values": [
[
1633597734,
"64.54166666666667"
],
[
1633598034,
"65.51666666666667"
]
]
}
]
}
}
output:
"65.51666666666667"

Generate json dump with dictionaries separated in commas

I'm in the process of generating a json file from python, however, i'd like separate the dictionaries via a comma after each loop, here's a portion of the code:
listA = [computer1,computer2]
listB = [computertype1,computertype2]
for computer, item in zip(listA,listB):
mydict = {
"Computertype": "somevalue",
"computer": [
computer
],
"targert": {
"item": item
}
},
The desired output should be:
[
{
"Computertype": "somevalue",
"computer": [
computer1
],
"targert": {
"item": computertype1
}
},
{
"Computertype": "somevalue",
"computer": [
computer2
],
"targert": {
"item": computertype2
}
}
]
So basically a comma right after the first end brace of the first loop: }, and all in one list, one bracket on top and one closing bracket at the bottom.
When running the code, it doesn't show the comma after each brace in the loop, and it does input each loop within a list automatically, any suggestions ?
What I get:
[ {
"Computertype": "somevalue",
"computer": [
computer1
],
"targert": {
"item": computertype1
}
}
]
[ {
"Computertype": "somevalue",
"computer": [
computer2
],
"targert": {
"item": computertype2
}
}]
I think this is what your looking for:
listA = [computer1,computer2]
listB = [computertype1,computertype2]
mylist = []
for computer, item in zip(listA,listB):
mydict = {
"Computertype": "somevalue",
"computer": [
computer
],
"target": {
"item": item
}
}
mylist.append(mydict)
print(mylist)

Incorrect JSON format

I am trying to construct a JSON formats follows but running into below error, what is wrong with the below format and how to fix it?
{
project_sha_list: [{
project: project1
sha: sha1
},
{
project: project2
sha: sha2
}
]
train: train1
}
ERROR:-
Error: Parse error on line 1:
{ project_sha_list: [{
--^
Expecting 'STRING', '}', got 'undefined'
JSON keys and string values must be in quotes like below, but if the value is integer,double,long then it should not be enclosed in quotes, and if you have multiple properties each property should end with , except last one
{
"project_sha_list": [
{
"project": "project1",
"sha": "sha1"
},
{
"project": "project2",
"sha": "sha2"
}
],
"train": "train1"
}
Your JSON should be:
{
"project_sha_list": [
{
"project": "project1",
"sha": "sha1"
},
{
"project": "project2",
"sha": "sha2"
}
],
"train": "train1"
}
You can validate it here
you keys and values both should be within "
Try this format:
{
"project_sha_list": [
{
"project": project1,
"sha": sha1
},
{
"project": project2,
"sha": sha2
}
],
"train": train1
}
The correct jain format is:
{
"project_sha_list": [
{ "project": "project1", "sha": "sha1" },
{ "project": "project2", "sha": "sha2" }
],
"train": "train1"
}

Expected name at xx:yy in JSON file in eclipse

"LOTGraphData": [
{
buildUnits:"1"
ScheduledUnits:2"
prodGroupId:"9288"
},
{
buildUnits:"1"
ScheduledUnits:2"
prodGroupId:"9289"
},
{
buildUnits:"1"
ScheduledUnits:2"
prodGroupId:"9280"
}
]
}
This is the end of JSON file.
You have to separate your key/value pairs by commas. And (as per your confirmation), the keys have to be wrapped in quotes. So the final result will look like:
"LOTGraphData": [
{
"buildUnits": "1",
"ScheduledUnits": "2",
"prodGroupId": "9288"
},
{
"buildUnits": "1",
"ScheduledUnits": "2",
"prodGroupId": "9289"
},
{
"buildUnits": "1",
"ScheduledUnits": "2",
"prodGroupId": "9280"
}
]

jmespath flatten multiple hash values

Ideally, I want to write a query that returns a flat list output: ["abc", "bcd", "cde", "def"] from the following JSON sample:
{
"l_l": [
[1,2,3],
[4,5,6]
],
"l_h_l": [
{ "n": [10,2,3] },
{ "n": [4,5,60] }
],
"l_h_m": [
{
"n": {
"1234": "abc",
"2345": "bcd"
}
}, {
"n": {
"3456": "cde",
"4567": "def"
}
}
]
}
The closest I can get is l_h_m[].n.* which returns the contents that I want as an unflattened list of lists:
[
[
"abc",
"bcd"
],
[
"cde",
"def"
]
]
jmespath lets you flatten lists of lists. Queries l_l[] and l_h_l[].n[] both returned flattened results, when the source json is structured that way.
Looks like your solution just required another flattening operator.
l_h_m[].n.*[]
returns
[
"abc",
"bcd",
"cde",
"def"
]