KRL: Parsing string as JSON - json

After using http:get(), I receive back a string from picking the "content" from the hash:
response = http:get(webservice_url, {"key1": value1, "key2": value2});
json_resp = response.pick("$..content");
However, since json_resp is a string and not an actual JSON object, I can't run a command like this:
value = json_resp.pick("$..string");
Is there a way to tell KRL that I want to parse json_resp as JSON? An eval() or something, perhaps?

The decode() operator does just what you want. It operates on a JSON string, attempting to convert it to a native KRL object. Note that KRL also has encode() which operates on a native KRL object and returns a JSON string representation of that object.
response = http:get(webservice_url, {"key1": value1, "key2": value2});
json_resp = response.pick("$..content").decode();
value = json_resp.pick("$..string");
// will work since json_resp is now a native KRL object

Related

Obtain value in json response

How do i write an expression to obtain the value "lastUpdated" in the below json response data
{"resourceType":"Parameters","parameter":[{"name":"medication","resource":{"resourceType":"Bundle","id":"956ffe6a-08ed-4cb6-82ca-41065a4a9923","meta":{"lastUpdated":"2020-08-24T19:09:18.5649325+00:00",
I have tried this but it does not work:
regex("\"lastUpdated\": \"(.*?)\"").saveAs("lastUpdated")
this also does not work:
jsonPath("$..[?(#.use==\"lastUpdated\")].value").saveAs("lastUpdated"))
Your input is a little cut off but here is what I've got:
myJSONString = '{"resourceType":"Parameters","parameter":[{"name":"medication","resource":{"resourceType":"Bundle","id":"956ffe6a-08ed-4cb6-82ca-41065a4a9923","meta":{"lastUpdated":"2020-08-24T19:09:18.5649325+00:00"}}}]}'
myJSONObject = JSON.parse(myJSONString)
myLastUpdated = myJSONObject.parameter[0].resource.meta.lastUpdated
console.log(myLastUpdated)
Basically you convert it from a json object into a javascript object. Then you can just traverse down the tree to your intended target.

How convert string to json or map to pair in dart if not valid json

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)

Deserialize multi JSON string with Play JSON library

I'm writing a server that need to parse JSON strings uploaded by clients. Currently I'm using Play JSON lib. For example:
import play.api.libs.json._
def parseJSON(jsonString: String) = {
val jsv = Json.parse(jsonString)
jsv
}
Considering a client uploaded a JOSN string of {"key1": 1}. After the server received the entire string, just simple invoke the parseJSONmethod, everything will be done.
However, if a client uploaded TWO JSON strings, {"key2": 2} and {"key3": 3}, and due to the bad network, these two JSON strings reach the server at the same time. The server will get a long string of {"key2": 2}{"key3": 3} (The server can not know it contains two JSON strings before parsing). if I invoke the parseJSON method and pass the entire string, only the first JSON value {"key2": 2} will be returned. The second one {"key3": 3} will be ignored.
So, how can I parse the second JSON string? Is there a way to know how many Chars are used when parsing the first JSON string?
val jsonString2 = """{
"key1": 1,
"key2": 2
}
{
"key3": 3,
"""
I think this is not valid JSON value, please make your question cleaner as possible so we can help :)

Erlang: Parse string to json

I have the following string:
"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",null],[\"234566767544\",\"FILE:application/pdf;456378;\",null],[\"233456767544\",\"FILE:application/pdf;456378;\",null]]}"
how do I parse it to a normal Json format?
meaning:
{"rows" :[
{"CNPJ":"17192451000170","PDF":"FILE:application/pdf;170286;","error":null},
{"CNPJ":"17192451000170","PDF":"FILE:application/pdf;170286;","error":null},
{"CNPJ":"17192451000170", "PDF":"FILE:application/pdf;170286;,"error":null"}
]}
or any other json format
This is already a valid JSON format.
If you just want to strip \ then you can simply:
(hbd#crayon2.yoonka.com)31> JsonOrg = <<"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",null],[\"234566767544\",\"FILE:application/pdf;456378;\",null],[\"233456767544\",\"FILE:application/pdf;456378;\",null]]}">>.
<<"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",null],[\"234566767544\",\"FI"...>>
(hbd#crayon2.yoonka.com)32> io:format("~s~n", [binary_to_list(JsonOrg)]).
{"headers":["CNPJ","PDF","error"],"rows":[["17192451000170","FILE:application/pdf;170286;",null],["234566767544","FILE:application/pdf;456378;",null],["233456767544","FILE:application/pdf;456378;",null]]}
ok
You can also parse back and forth between Json and Erlang. I tested that with the yajler decoder:
(hbd#crayon2.yoonka.com)43> {ok, Parsed} = yajler:decode(<<"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",null],[\"234566767544\",\"FILE:application/pdf;456378;\",null],[\"233456767544\",\"FILE:application/pdf;456378;\",null]]}">>).
{ok,[{<<"headers">>,[<<"CNPJ">>,<<"PDF">>,<<"error">>]},
{<<"rows">>,
[[<<"17192451000170">>,<<"FILE:application/pdf;170286;">>,
undefined],
[<<"234566767544">>,<<"FILE:application/pdf;456378;">>,
undefined],
[<<"233456767544">>,<<"FILE:application/pdf;456378;">>,
undefined]]}]}
(hbd#crayon2.yoonka.com)44> Json = binary:list_to_bin(yajler:encode(Parsed)).
<<"{\"headers\":[\"CNPJ\",\"PDF\",\"error\"],\"rows\":[[\"17192451000170\",\"FILE:application/pdf;170286;\",\"undefined\"],[\"2345667675"...>>
Yajler is an Erlang NIF so it is using a C library, in this case called yajl, to do the actual parsing, but I imagine a similar result you would get from other Erlang applications that can parse JSON.

difference between json string and parsed json string

what is the difference between json string and parsed json string?
for eg in javascript suppose i have a string in the json format say [{},{}]
parsing this string will also produce the same thing.
So why do we need to parse?
It's just serialization/deserialization.
In Javscript code you normally work with the object, as that lets you easily get its properties, etc, while a JSON string doesn't do you much good.
var jsonobj = { "arr": [ 5, 2 ], "str": "foo" };
console.log(jsonobj.arr[1] + jsonobj.str);
// 2foo
var jsonstr = JSON.stringify(jsonobj);
// cannot do much with this
To send it to the server via an Ajax call, though, you need to serialize (stringify) it first. Likewise, you need to deserialize (parse) from a string into an object when receiving JSON back from the server.
Great question. The difference is transfer format.
JSON is only the 'Notation' of a JavaScript Object, it is not actually the JavaScript 'object-literal' itself. So as the data is received in JSON, it is just a string to be interpreted, evaluated, parsed, in order to become an actual JavaScript 'Object-Literal.
There is one physical difference between the two, and that is quotation marks. It makes sense, that JSON needs to be a string to be transferred. Here is how:
//A JavaScript Object-Literal
var anObj = { member: 'value'}
//A JSON representation of that object
var aJSON = { "member":"value" }
Hope that helps. All the best! Nash
I think a parsed json string should be the string data into the actual javascript objects and data arrays (or whichever language the json string contains)
The JSON object contains methods for parsing JSON and converting values to JSON.
It can't be called or constructed, and aside from its two method properties it has no interesting functionality of its own.
JSONParser parser = new JSONParser();
Object object = parser.parse(Message.toString());
JSONObject arObj = (JSONObject) object;