i have a json object which contains json data with a key. now i want to extract value from that json object like name, address etc and store them to variables.
controller
json_arr = new JSONArray(j_str);
int count = json_arr.length();
json_o.put("user", json_arr);
j_str contains following data
[{"Bollywood":[{"actor":[{"name":"AA","gender":"Male"},{"name":"BB","gender":"Male"}]}]},{"Hollywood":[{"actor":[{"name":"CC","gender":"Male"},{"name":"DD","gender":"Male"}]}]}]
now it is converted to json object -- json_o ,, putting a key --- "user". now how can get a specific data such as 2nd actor name from hollywood. (i.e value DD). after then store that to a string.
Short answer: Use Jackson to map the json string to a java object, and then extract that value as a variable.
Here is a quick guide on doing this with jackson: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
Related
Here the json data. [1,"GarageC","NUR841",1611241200]
how do i get the GarageC out from json.decode?
This is array you can retrieve value from array by index basis like array_name[index]
I have a struct:
type person struct{
FirstN [10]byte
Last Name [10]byte
Address [15]byte
zip [6]byte
}
Then I have Map
xyz = [01:aaaaaaaaaabbbbbbbbbbccccccccccccccc123456]
This map is exactly the same as my struct. Basically if I overlay my structure with the string in the map it is an exact match.
I am trying to get a JSON string for this data, using Marshal. But for that (as I understand it) I need to update the data in the map into the struct and then pass the structure pointer to Marshal
But I cannot find any way to get the data from the map with key '01' which is a string and initialize my structure with it. I do not want to add a code to update each field in the structure by parsing the string from the map. Is there a way to do it or hardcoding is the only option.
Also is there a way to create a JSON string from a map string directly?
That's not json data. However, the binary.Read function will decode arbitrary fixed sized values from a binary stream following the struct layout.
data := []byte("aaaaaaaaaaaaaaaaaaaaccccccccccccccc123456")
err := binary.Read(bytes.NewReader(data), binary.LittleEndian, &p)
https://play.golang.org/p/-I_XhUCvNN
I am trying to use com.jayway.jsonpath.JsonPath to read key/values from a JSON string:
String contentAsString = mvcResult.getResponse().getContentAsString();
System.out.println(contentAsString);
Object value = JsonPath.read(contentAsString, "$.key");
But I get the error:
Expected to find an object with property ['key'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
Printing contentAsString gives:
[{"firstName":"N","key":"mykey"}]
Is contentAsString not valid JSON?
The blob that you posted is not a valid JSON Object, however, it IS a valid JSON Array.
To read this using JsonPath, try using this:
Object value = JsonPath.read(contentAsString, "$[0].key");
This will get the value of the key object, from the 0th element of the initial array.
I'm testing data lake for an application I am developing. I'm new to U-SQL and data lake and am just trying to query all records in a JSON file. Right now, It's only returning one record and I'm not sure why because the file has about 200.
My code is:
DECLARE #input string = #"/MSEStream/output/2016/08/12_0_fc829ede3c1d4cf9a3278d43e7e4e9d0.json";
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
#allposts =
EXTRACT
id string
FROM #input
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor();
#result =
SELECT *
FROM #allposts;
OUTPUT #result
TO "/ProcessedQueries/all_posts.csv"
USING Outputters.Csv();
Data Example:
{
"id":"398507",
"contenttype":"POST",
"posttype":"post",
"uri":"http://twitter.com/etc",
"title":null,
"profile":{
"#class":"PublisherV2_0",
"name":"Company",
"id":"2163171",
"profileIcon":"https://pbs.twimg.com/image",
"profileLocation":{
"#class":"DocumentLocation",
"locality":"Toronto",
"adminDistrict":"ON",
"countryRegion":"Canada",
"coordinates":{
"latitude":43.7217,
"longitude":-31.432},
"quadKey":"000000000000000"},
"displayName":"Name",
"externalId":"00000000000"},
"source":{
"name":"blogs",
"id":"18",
"param":"Twitter"},
"content":{
"text":"Description of post"},
"language":{
"name":"English",
"code":"en"},
"abstracttext":"More Text and links",
"score":{}
}
}
Thank you for the help in advance
The JsonExtractor takes an argument that allows you to specify which items or objects are being mapped into rows using a JSON Path expression. If you don’t specify anything it will take the top root (which is one row).
You want every one of the items in the array, so you specify it as:
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor("[*]");
Where [*] is the JSON Path expression that says give me all the elements of the array which in this case is the top-level array.
If you have a JSON node in your field called id, your original script posted in the question would return the node with name "id" under the rootnode. To get all the nodes, your script will be structured as
#allposts =
EXTRACT
id string,
contenttype string,
posttype string,
uri string,
title string,
profile string
FROM #input
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor();
Please let us know if it works. The alternative would be to extract it using a native extractor to read it all in a string (as MRys mentioned, as long as your JSON is under 128 KB this would work).
#allposts =
EXTRACT
json string
FROM #input
USING Extractors.Text(delimiter:'\b', quoting:false);
I want to create a Json object from the below string using Java
{"attributes":{"numvcpus":1,"memsize":3072},"id":"OS Node","type":"image"}
If I know that the string will be what it is , I can create a Json object like this
JsonObject jObj = Json.createObjectBuilder()
.add("attributes",Json.createObjectBuilder()
.add("numvcpus",1)
.add("memsize",3072))
.add("id", "OS node")
.add("type": "image").build();
But the problem here is I don't know how many values will come in the nested json object i.e, the json object which is the value for "attributes" field. There may be additional fields like "disksize": 30, "lcpu": 4 etc . Is there any way to dynamically create Json objects, using for loop, while loop ?