I have a string column which is stored in json format
{"name":
{"type":"json",
"payload":"
{\"id\":
\"123\",
\"activities\": [....]
}
}
}
I need to parse it into json format.
and then turn it into map of <name, payload> where payload is rest of the json.
"name" is a key and not a constant string.
what is the best way to do this?
Related
Upon using a multi-line codec, my single json object contains escape backslashes for each quote used for the key-value pairs.
Kindly help me access/extract individuaL key pairs from such a json using logstash plugins.
"msg" => "{ \"message\": \"type=SYSCALL msg=audit(23:45:56): arch=c000003e syscall=1 success=yes exe=\"/usr/sbin/sshd\" subj=adfghjk SYSCALL=write AUID=\"awx\" UID=\"root\" GID=\"root\"\", \"log\": { \"offset\": 9274863, \"file\": { \"path\": \"/var/log/audit/audit.log\" } }}"
Regards
I have JSON content inside another JSON that I need to extract as it is, without parsing its contents:
{
"id": 555,
"name": "aaa",
"JSON": "{\r\n \"fake1\": {},\r\n \"fake2\": \"bbbb\",\r\n \"fake3\": \"eee\" \r\n}",
"after1": 1,
"after2": "test"
}
When I use JSON Extractor with JSON Path expression:
$.JSON
It returns:
"{
"fake1": {},
"fake2": "bbbb",
"fake3": "eee"
}"
when I need to get the raw string:
"{\r\n \"fake1\": {},\r\n \"fake2\": \"bbbb\",\r\n \"fake3\": \"eee\" \r\n}"
I think you need to switch to JSR223 PostProcessor instead of the JSON Extractor and use the following code:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData()).JSON
vars.put('rawString', org.apache.commons.text.StringEscapeUtils.escapeJson(json))
You will be able to refer the extracted value as ${rawString} where required.
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy: What Is Groovy Used For?
console.log(JSON.stringify(data.JSON))
Here data is your given JSON data.
At first, you have to extract your JSON/data. Then you have to stringify the JSON data using JSON.stringify().
The confusing fact you have done here is that you named your key in the JSON object as "JSON".
In js when you extract a JSON object if there is another nested JSON object you will always get JSON data by just data.key_name
where data is JSON data
key is for Nested JSON key
I want to get a nested field in a json string using JSONPath.
Take for example the following json:
{
"ID": "2ac464eb-352f-4e36-8b9f-950a24bb9586",
"PAYLOAD": "{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
}
If I want to extract the #type field, I expect to do it like this
$.PAYLOAD.#type
But that doesn't seem to work..
Also tried this:
$.PAYLOAD['#type']
Do I need to use escape chars or something?
Posting my comment as an answer
"{\"#type\":\"Event\",\"id\":\"baf223c4-4264-415a-8de5-61c9c709c0d2\"}"
Isn't JSON, it's a string containing encoded JSON.
Since JsonPath can't decode such string, you'll have to use a language of your desire to decode the string.
Eg: Decoding JSON String in Java
What is the best method to convert string to json in my example or map key value.
var str = "created_at: 2020-07-09T06:32:19Z, entry_id: 9510, field1: null, field2: 19.00"
json.decode(str);
Error:
FormatException: SyntaxError: Unexpected token c in JSON at position 0
To convert Map/String -> JSON we use json.encode()
String str = "This is a String";
//for JSON format
json.encode(str);
Map<string,int> myMap = {"a":1};
//to convert into JSON
json.encode(myMap); // => "{"a":1}" JSON form
Similarly to convert JSON -> Map/String we use json.decode()
While json.decode is the correct method to use, your string is not valid JSON. Your string in valid json would look something like this:
{
"created_at": "2020-07-09T06:32:19Z",
"entry_id": 9510,
"field1": null,
"field2": 19.0
}
If you have a Map that you want to encode to JSON, use json.encode.
If this is your first time working with JSON, you might want to check out a tutorial on the syntax first (maybe this)
I'm using JSON Path PostProcessor with path expressions to store a JSON object from a JSON response, but when I later retrieve the variable, it has been reduced to string with key - pair values. So I don't know that was a string or number.
Example:
Response looks like this
{
.
.
"currency" : {
"code" : "AUD",
"name" : "Australian Dollars",
"symbol" : "$"
},
.
}
Using the path expression, I find currency and save it.
However, when I use it in a HTTP Request body data ("currency" : ${currency},),
it comes like this:
"currency" : {code=AUD, name=Australian Dollars, symbol=$},
How do I get the JSON Path PostProcessor to save the JSON object 'as is" without losing the data type details? Should I be using a different approach instead of JSON Path?
I would recommend switching to JSR223 PostProcessor and Groovy language, this way you will have full control of what is going on and be able to do what you want.
Assuming you have the following JSON response:
{
"currency": {
"code": "AUD",
"name": "Australian Dollars",
"symbol": "$"
}
}
the relevant Groovy coode will be something like:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def currency = json.currency
def currencyString = new groovy.json.JsonBuilder(currency).toPrettyString()
vars.put('currency', currencyString)
log.info(currencyString)
Demo:
References:
Groovy - Learn
Groovy - Parsing and producing JSON
Groovy is the New Black