Groovy: escape lower level JSON - json

I need to keep the first level JSON keys and convert the values to escaped strings, but only in case the values are also JSON objects. How can this be done in Groovy?
Input sample:
{
"a": "1",
"b": {
"c": "2",
"d": {
"e": "3"
}
},
"f": "4"
}
Desired result:
{
"a": "1",
"b": "{ \"c\": \"2\", \"d\": { \"e\": \"3\"} }",
"f": "4"
}

If you use JsonSlurper to parse the input JSON, then any nested JSON object will be represented as a LazyMap. You can use this information to collect all entries from the parsed JSON object (which is also a map) and convert any map object to its JSON string representation. You can convert any value to a JSON string representation using groovy.json.JsonOutput.toJson(object) method.
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def input = '''{
"a": "1",
"b": {
"c": "2",
"d": {
"e": "3"
}
},
"f": "4"
}'''
def json = new JsonSlurper().parseText(input)
def escaped = json.collectEntries { k,v ->
[(k): v instanceof Map ? new JsonOutput().toJson(v) : v]
}
def output = new JsonOutput().prettyPrint(JsonOutput.toJson(escaped))
println output
Output:
{
"a": "1",
"b": "{\"c\":\"2\",\"d\":{\"e\":\"3\"}}",
"f": "4"
}

Related

This is json file named as 'abc' . I want to access file after that i want to access values of a,b,c,d,e,f . How can I access in groovy language?

{
"Product": [{
"a": "e3ae148d8bc2c5fe366c38da"
},
{
"b": "d211c6e7ed5bd8b4e9316a74085"
},
{
"c": "74be4f1b3b0fa5af77287780"
},
{
"d": "89856f4f139a84c98fb98b8b39c32e2"
},
{
"e": "7bd784e7d3b8490ed614345989a5"
},
{
"f": "d169f6a4a932841b12bdf8ccded57"
}
]
}
Use JsonSlurper,
File file = new File('abc')
String fileContent = file.text
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(fileContent)
println result.Product[0].a //etc.
...

How can I convert to Json Object to Json Array in Karate?

I want to convert Json Object to Json Array in Karate to use 'match each' func.
I am getting to ('match each' failed, not a json array) error when I use match each func with Json Object.
Here is My Json Object:
{
{ "a": "q"
"b": "w",
"c": "t"
},
{ "a": "x"
"b": "y",
"c": "z"
}
}
And here is what I need:
[
{
{ "a": "q"
"b": "w",
"c": "t"
},
{ "a": "x"
"b": "y",
"c": "z"
}
}
]
Try this approach, using embedded expressions: https://github.com/intuit/karate#embedded-expressions
* def foo = { a: 1 }
* def list = [ '#(foo)' ]
* match each list == foo

How to apply left join on two JSON objects in groovy script

I have two JSON files:
first.json
[
{"a":"1", "b": "tmp"},
{"a":"2", "b": "tmp"},
{"a":"3", "b": "tmp"}
]
second.json
[
{"c":"1", "d": "tmp"},
{"c":"2", "d": "tmp"},
{"c":"4", "d": "tmp"}
]
output.json
[
{"a":"1", "b": "tmp", "c": "1" , "d": "tmp"},
{"a":"2", "b": "tmp", "c": "2" , "d": "tmp"},
{"a":"3", "b": "tmp", "c": "" , "d": ""}
]
I want to apply left join on two json files first.json and second.json on basis of two fields - "a" of first.json and "c" of second.json to get the output as output.json. How can I achieve the same using Groovy Script?
NOTE: I would like to achieve this in a single line if possible.
You would need to do something like this:
def firstJson = '''[
{"a":"1", "b": "tmp"},
{"a":"2", "b": "tmp"},
{"a":"3", "b": "tmp"}
]'''
def secondJson = '''[
{"c":"1", "d": "tmp"},
{"c":"2", "d": "tmp"},
{"c":"4", "d": "tmp"}
]'''
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
def slurpy = new JsonSlurper()
def first = slurpy.parseText(firstJson)
def second = slurpy.parseText(secondJson)
def result = first.collect { f ->
f + (second.find { it.c == f.a } ?: second[0].keySet().collectEntries { [it, ''] })
}
println JsonOutput.toJson(result)

KarateException Missing Property in path - JSON

I was trying to match particular variable from response and tried as below. But im getting error saying KarateException Missing Property in path $['Odata']. My question is: how we can modify so that we won't get this error?
Feature:
And match response.#odata.context.a.b contains '<b>'
Examples:
|b|
|b1 |
|b2 |
Response is
{
"#odata.context": "$metadata#Accounts",
"a": [
{
"c": 145729,
"b": "b1",
"d": "ON",
},
{
"c": 145729,
"b": "b2",
"d": "ON",
}
]
}
I think you are confused with the structure of your JSON. Also note that when the JSON key has special characters, you need to change the way you use them in path expressions. You can try paste the below in a new Scenario and see it work:
* def response =
"""
{
"#odata.context": "$metadata#Accounts",
"a": [
{
"c": 145729,
"b": "b1",
"d": "ON",
},
{
"c": 145729,
"b": "b2",
"d": "ON",
}
]
}
"""
* match response['#odata.context'] == '$metadata#Accounts'
* match response.a[0].b == 'b1'
* match response.a[1].b == 'b2'

Julia | DataFrame conversion to JSON

I have a dataframe in Julia like df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"]). I have to convert it into a JSON like
{
"nodes": [
{
"A": "1",
"B": "M"
},
{
"A": "2",
"B": "F"
},
{
"A": "3",
"B": "F"
},
{
"A": "4",
"B": "M"
}
]
}
Please help me in this.
There isn't a method in DataFrames to do this. In a github issue where the following snippet, using JSON.jl, is offered as a method to write json:
using JSON
using DataFrames
function df2json(df::DataFrame)
len = length(df[:,1])
indices = names(df)
jsonarray = [Dict([string(index) => (isna(df[index][i])? nothing : df[index][i])
for index in indices])
for i in 1:len]
return JSON.json(jsonarray)
end
function writejson(path::String,df::DataFrame)
open(path,"w") do f
write(f,df2json(df))
end
end
JSONTables package provides JSON conversion to/from Tables.jl-compatible sources like DataFrame.
using DataFrames
using JSONTables
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
jsonstr = objecttable(df)