How to loop and get the json object - json

{
"result": [
{
"id": "a258377906705d889422fd0b41c324b8",
"coordinate": {
"London": {
"x": 65.565709,
"y": 98.931235
},
"New_York": {
"x": 37.59751,
"y": 47.448718
}
}
}
]
}
If I have a json like the above one,
how can I loop to get the x,y coordinate and if i get more data to get, how can I get it?
If I want to also get the London and New York to add to an array list, how can I do it (cannot directly add the name because there are more than two data)?

You can parse it and then use it using JSON.parse( insert json here ).
Basically what it does is that it takes your JSON string and converts it into a usable JSON.
For adding new objects into another object, I recommend using Object.assign().
You can refer the details here: Link
Here is an example given from the site:
var o1 = { a: 1 };
var o2 = { b: 2 };
var o3 = { c: 3 };
var obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
Object.assign() takes a target object as the first parameter and modifies it with the additional parameters that we're provided.
In your case, you can update your object like this:
var countryCoord = JSON.parse(yourjsondata); // parse the json that you included
Object.assign(countryCoord, newCoord, newCoord2,...etc); //this would update countryCoord alone
Online parser: Link

You have to handle the json and understand where you are getting the JSONObject and where you are getting the JSONArray, On basis of that you can parse the JSON accordingly.
Please refer the link for more information on parsing json object with nested items in Java:
Retrieving nested arrays values with java

Related

Trouble extracting JSON data in Flutter

I'm trying to render a list of elements in Flutter and the list data is coming from a REST API. However, I'm having trouble understanding how to parse this data correctly. I want the parsed data to look like this:
['Chicken roll', 'Fresh salads', 'Wrape', 'Pizza', 'Fried Chicken', 'Veggie', 'Burgers']
This is the JSON object:
{
"status":true,
"error_message":"",
"success_message":"Task completed successfully.",
"data":[
{
"id":1064,
"name":"Chicken roll"
},
{
"id":1061,
"name":"Fresh salads"
},
{
"id":1059,
"name":"Wrape"
},
{
"id":1057,
"name":"Pizza"
},
{
"id":1055,
"name":"Fried chicken"
},
{
"id":1053,
"name":"Veggie"
},
{
"id":1051,
"name":"Burgers"
}
]
}
How do I achieve this? My current code is as follows:
var _categoryResponse;
var _categoryData;
var categoryData;
List<String> allCategories;
Future fetchCategories() async {
_categoryResponse = await http.get(_categories);
_categoryResponse = jsonDecode(_categoryResponse.body);
_categoryData = _categoryResponse['data'].toString();
categoryData = categoryDataFromJson(_categoryData);
}
List<String> listOfNames = [];
List<dynamic> data = json['data'] as List;
data.forEach((item) => listOfNames.add(item['name']));
print(listOfNames);
Gives the following output:
[Chicken roll, Fresh salads, Wrape, Pizza, Fried chicken, Veggie, Burgers]
I'm assuming the variable json above contains your parsed JSON object (as given in the example).
Not sure why you're converting JSON to String as its much easier to work with the dynamic map, at least when it comes to fetching particular attributes like the name of each object within the data list.
Basically, the data key maps to a list of objects with attributes id and name. Hence, you can run a forEach() on the list obtained and fetch the name for each object in it.

Stream analytics parse json, same key can be array or not

A XML is converted to JSON and sent to an EventHub and then a Stream Analytics process it.
The problem is when XML uses the same tags name it gets converted to a list on the JSON side, but when there is only one tag is not converted to a list. So the same tag can be an array or not.
Ex:
I can receive either:
{
"k1": 123,
"k2": {
"l1": 2,
"l2": 12
}
}
or:
{
"k1": 123,
"k2": [
{
"l1": 2,
"l2": 12
},
{
"l1": 3,
"l2": 34
}
]
}
I can easily deal with the first scenario and the second scenario independently, but I don't know how to deal with both at the same time, is this possible?
Yes, it is. If you know how to deal with each of the cases individually, I will just suggest an idea of how you can make the distinction between these two cases, before you treat them individually.
Essentially, the idea is to check if the field is an array. What I did was, I wrote a UDF function in javascript that returns "true"/"false", if the passed object is an array:
function UDFSample(arg1) {
'use strict';
var isArray = Array.isArray(arg1);
return isArray.toString();
}
here is how you can use this in the group query:
with test as (SELECT Document from input where UDF.IsArray(k2) = 'true')
now "test" contains items that you can treat as an array. The same you can do for the case where k2 is just an object.

How to access nested elements in json in flutter?

This is the json data structure used in flutter
flutter: {
"members": [
{
"firstName": "Michael"
},
{
"firstName": "Jennifer"
},
{
"firstName": "Lisa"
}
]
}
To access the above data I used the below code
var parsedData = json.decode(state.successResponse);
var name = parsedData['members'];
I am not able to access the inner field of firstName in the above json.
How should I do that?
Since members is JSON Array you can convert it to dart List type and then you can operate on the list (adding, removing, accessing elements by index).
You can do something like this -
List members = parsedData['members'];
//You can access members by index
print(members[1]); //prints {firstName: Jennifer}
print(members[1]['firstName']); //prints Jennifer
//Or you can iterate over the list
members.forEach((member){
print(member['firstName']);
});
Hope this helps!

Comparing Json data. Python 3

I have the following Json file and I need to compare data to see how many times each value repeat itself. The problem is, I have no idea about handling Json. I don't want the answer to my exercise, I want to know how to access the data. Json:
{
"tickets": [
{
"ticket_id": 0,
"timestamp": "2016/05/26 04:47:02",
"file_hash": "c9d4e03c5632416f",
"src_ip": "6.19.128.119",
"dst_ip": "145.231.76.44"
},
{
"ticket_id": 1,
"timestamp": "2017/05/28 16:14:22",
"file_hash": "ce8a056490a3fd3c",
"src_ip": "100.139.125.30",
"dst_ip": "145.231.76.44"
},
{
"ticket_id": 2,
"timestamp": "2015/08/23 03:27:10",
"file_hash": "d17f572496f48a11",
"src_ip": "67.153.41.75",
"dst_ip": "239.168.56.243"
},
{
"ticket_id": 3,
"timestamp": "2016/02/26 14:01:33",
"file_hash": "3b28f2abc966a386",
"src_ip": "6.19.128.119",
"dst_ip": "137.164.166.84"
},
]
}
If this is a string representation of the object, first you need to set a variable and parse the string to have object you can work with.
jsonString = "{...your json string...}"
Then parse the string,
import json
jsonObject = json.loads(jsonString)
To access the data within it's like any other js object. Example :
jsonObject.tickets[0].timestamp
would return "2016/05/26 04:47:02"
tickets is the key within the jsonObject, 0 is the index of the first object in the list of tickets.
You can use the built-in "json" library to parse your file into an object:
import json
f = open('myfile.json','r')
tickets = json.loads(f.read())
This will return a "tickets" object. How you "compare" (or what exactly you compare) is up to you.

Can someone provide me an example on how to convert this data format to JSON?

How do i convert json text file to..
var nested_obj = { pic: "jaedongImage", name: "ananth", team: "evil geniuses", server: "NA" };
My text file as below..
{
"data": [
{
"pic": "jaedongImage,"
"name": "ananth",
"team": "evil geniuses",
"server": "N/A"
}
]
}
It's unclear what you're asking, but: The JSON depicts an object with one property, data, which refers to an array. The array has one entry, which is the object you wanted. From your initial code sample, it looks like you're using JavaScript, so: Assuming you're receiving text (a string) in JSON format, you would parse it using JSON.parse, and then get at that object via data[0]:
var obj = JSON.parse(text);
var pic = obj.data[0];