I am using json parser lib written in C to parse JSON objects. The lib link is : https://github.com/udp/json-parser.
The json object/string, which I am trying to parse is :
{"video_id": 105, "st": "S3", "processing" : [{"start" : "1", "end" : "2"}]}}
"processing" contains another JSON object.
I have parsed the first three items. But I am not able to figure out a way to parse the "processing" json object. I am using following code:-
if (!strcmp(json->u.object.values[i].name, "video_id"))
{
video_id=json->u.object.values[i].value->u.integer;
}
.
.
if (!strcmp(json->u.object.values[i].name, "processing"))
{
printf("\nNumber of JSON OBJECTS : %d\n", json->u.object.values[i].value->u.object.length);
}
json is the parsed object obtained via calling the lib on the JSON string. Can anyone guide me how to handle the nested object ?
Any help will be really appreciated
My complete code is :
json_value *json;
json_char *json_object="{\"video_id\": 105, \"st\": \"S3\", \"processing\" : [{\"type\" : \"clipping\"},{\"fese\" : \"dipping\"}]}";
printf("%s",json_object);
//json_value * json_parse (const json_char * json,
// size_t length);
json=json_parse(json_object, strlen(json_object));
// json_type json_object;
printf("\n%s\n",json->u.object.values[0].name);
printf("\t%d\n",json->u.object.values[0].value->u.integer);
printf("\n%s\n",json->u.object.values[2].name);
printf("\t%d\n",json->u.object.values[2].value->u.object.length);
printf("\t%s\n",json->u.object.values[2].value->u.object.values[0].name);
From the documentation, API field::
The type field of json_value is one of:
json_object (see u.object.length, u.object.values[x].name, u.object.values[x].value)
json_array (see u.array.length, u.array.values)
json_integer (see u.integer)
json_double (see u.dbl)
json_string (see u.string.ptr, u.string.length)
json_boolean (see u.boolean)
json_null
So, check the type field of the "processing" value. If found to be json_array, do a json_parse for the array to get a new json_value. Now this json_value will provide you with the nested JSON objects.
Take these as reference:
js_v->u.object.values[1].value->u.array.values[0]->type
js_v->u.object.values[1].value->u.array.values[0]->u.string.ptr
I've used them to reference to a string element inside an array:
{"t":"type","d":["element1","element2","element3"]}
In your case, I think you should to repeat the structure like this:
js_v->u.object.values[2].value->u.array.values[0]->u.object.values[0]->u.string.ptr
luck!
Related
I am trying to convert the below Array in to the JSON, trying by iterating through the Array and transforming. The array looks like below
Quote_Sent_To__r = [
{
Quote__c=0Q02D05XGQSA2,
Id=a1H2D0m94QUAQ,
type=QuoteSentTo__c
},
{
Quote__c=0Q02D00XGQSA2,
Id=a1H2D00000AQ,
type=QuoteSentTo__c
}
]
I have stored the array in to the variable quoteSentToList and iterating through the for loop
Within each iteration I need to get the JSON like
{
"Quote__c": "0Q02D05XGQSA2"
}
So this can be passed to the Salesforce Update operation. I tried like
%dw 2.0
output application/json
var item = vars.quoteSentToList[counter]
---
{
"Quote__c" :payload.Id
}
It errors saying
Reason: Unable to resolve reference of: counter..
Scripting language error on expression 'payload'. Reason: Unable to resolve reference of: payload..
This is my first project and any help is greatly appreciated
Error
""Unexpected character 'v' at quoteSentToList#[1:1] (line:column), expected false or true or null or {...} or [...] or number but was , while reading quoteSentToList as Json.
1| vars.existingQuote[0].Quote_Sent_To__r ^" evaluating expression: "%dw 2.0 output application/json
---
vars.quoteSentToList map { Quote__c: payload.Id, Id: $.Id }"."
counter is a Mule variable, not a DataWeave variable. You need to use the prefix vars. to reference it inside DataWeave scripts: vars.counter.
Alternatively, instead of using a <foreach> scope, you can transform the entire array at once and then use each element as needed:
%dw 2.0
output application/json
---
vars.quoteSentToList map { Quote__c: $.Id }
Output:
[
{
"Quote__c": "a1H2D0m94QUAQ"
},
{
"Quote__c": "a1H2D00000AQ"
}
]
I am using the library parson to construct JSON messages. I did not figure it out how to make sub-key without using the dotfonction. The JSON, I would like to make is :
"received_data": {
"X-YY": {
"pm10": 1,
"pm25": 2,
"pm100": 3
},
"X-ZZ": {
"pm10": 1,
...
}
}
From now I only found how to make the JSON Array to get "pm10,pm25,pm100" values by using this code :
//creating a sub Json_Array
JSON_Value *branch = json_value_init_array();
JSON_Array *leaves = json_value_get_array(branch);
//creating measurement Json
JSON_Value *leaf_value = json_value_init_object();
JSON_Object *leaf_object = json_value_get_object(leaf_value);
json_object_set_string(leaf_object,"PM10:",val);
json_object_set_string(leaf_object,"PM25:",val1);
json_object_set_string(leaf_object,"PM100:",val2);
json_array_append_value(leaves,leaf_value);
I don't really understand how to link the keys between them using the parson library ? Can someone help me figure it out ?
I have been following the instructions here:
https://www.codenameone.com/javadoc/com/codename1/io/JSONParser.html to retrieve a value from a json file.
I have managed to read the top level value of my json content - however I cannot see how to read the value of a nested tag e.g. using this file ...
{
"glossary":{
"title":"example glossary",
"GlossDiv":{
"title":"S",
"GlossList":{
"GlossEntry":{
"ID":"SGML",
"SortAs":"SGML",
"GlossTerm":"Standard Generalized Markup Language",
"Acronym":"SGML",
"Abbrev":"ISO 8879:1986",
"GlossDef":{
"para":"A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso":[
"GML",
"XML"
]
},
"GlossSee":"markup"
}
}
}
}
}
Please can someone show me how to get to the value of "para" above?
Thanks
After parsing your json data based on this, you can use Result to read deep into the json content:
import com.codename1.processing.Result;
...
Map<String, Object> data = json.parseJSON(r);
Result result = Result.fromContent(data);
String id = result.getAsString("glossary/GlossDiv/GlossList/GlossEntry/ID");
String para = result.getAsString("glossary/GlossDiv/GlossList/GlossEntry/GlossDef/para");
I have an array in which data from db is stored. I have converted this array into JSON by stringify which results in json like this:
var view_data = [{ f o o = " boo " , t e x t = "t e s t " }];
When i try to access this foo object via for loop like this :
for(var d=0; d<view_data.length; d++)
{
print view_data[d].foo;
}
It gives undefined. I think this is not actually json object is array of strings.
Please help me to access the key-value pair.
You seem to be fuzzy on the meaning of JSON. JSON is a string-based format used for exchanging data. It's not used, and is irrelevant, in the course of normal programming in JavaScript, unless you're sending data to somewhere, or receiving data from somewhere, in JSON format.
I have converted this array into JSON by stringify which results in json like this:
var view_data = [{ f o o = " boo " , t e x t = "t e s t " }];
JSON.stringify takes a JavaScript object and converts it into a string-based format. Therefore this is impossible. JSON.stringify would always return a string, not a (broken) JS object as you have shown.
In your sample data, you have shown extra spaces in the property names: "f o o" instead of "foo". If in fact that's how you have attempted to specify your JS object, it won't work at all. It's invalid syntax. It is also invalid syntax to use an equal sign; you need a colon.
If you define your JS object correctly, then the code you showed will work fine:
var view_data = [{foo: "boo" , text: "test"}];
for (var d=0; d<view_data.length; d++) {
print view_data[d].foo;
}
Accessing json generated by stringify
You don't access JSON generated by stringify. JSON is a string format meant only use in communicating with some external system.
how should then access the array as json object
There is no such thing as a JSON object. There is JSON, which is a string; and there are JavaScript objects.
For more information on JavaScript objects vs. JSON, see How to parse variables to JSON Object (Javascript).
I have an generic array of objects:
static Array<Level> allLevels = new Array<Level>();
Now I want to encode it to json like this:
Json json = new Json();
String levelsJsonString = json.prettyPrint(allLevels);
The problem occurs when I'm trying to reverse this process:
allLevels = json.fromJson(Level.class, levelsJsonString);
It is raising that Array and Level are incompatible types. How to do this?
As far as I know there's always problem with passing generic type class to a json fromJson() method.
The simpliest workarround is just to wrap Array type like:
public class Levels
{
public static Array<Level> allLevels = new Array<Level>();
}
Then you can use it just like
Levels.allLevels.add( new Level() ); //etc...
and retrieve it from JSON like
... = json.fromJson(Levels.class, jsonLevels);
passing the wrapping class class object to fromJson method. The same you are doing hen converting to JSON - pass wrapping class not just Array object
String levelsJsonString = json.prettyPrint(Levels.allLevels);