Loop through JSON data with unspecific index key - arrays

I have this json file below and I want to be able to loop through and print the corresponding value. e.g If I send a params "en", I should be able to print Unjha.
[
{
"apmc_code":1000,
"en":"Unjha",
"mr":"उंझा",
"hi":"उंझा",
"pa":"ਉਂਝਾ",
"gu":"ઊંઝા",
"te":"ఉంఝా",
"kn":"ಉಂಜಾ",
"ta":"உன்ஜா",
"ml":"ഉൻഝാ"
},
{
"apmc_code":1001,
"en":"Jamnagar",
"mr":"जामनगर",
"hi":"जामनगर",
"pa":"ਜਾਮਨਗਰ",
"gu":"જામનગર",
"te":"జామ్‌నగర్",
"kn":"ಜಾಮ್‌ನಗರ",
"ta":"ஜாம்நகர்",
"ml":"ജാംനഗർ"
},
]
Any help?

iterate over outer-list
iterate over item-map
print map.key = map.value

Related

Patch request in SCIM with Azure AD

How should I handle the following PATCH request, for a user that when initially added didn't have any address (not even an empty addresses array)?
{
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:PatchOp"
],
"Operations": [
{
"op": "Add",
"path": "addresses[type eq \"work\"].formatted",
"value": "Columbus"
}
]
}
Should I "proactively" create an addresses array, with a single value as following (what seems a very bad solutions)?
{"type": "work", formatted: "Columbus"}
I would expect a patch request that looks like:
{
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:PatchOp"
],
"Operations":[{
"op":"add",
"value":{
"addresses":[
{
"formatted":"Columbus",
"type":"work"
}
]
}]
}
If no array exists yet, then you should create the array and then add the value to the array. You can set it ahead of time to be an empty array, or can leave the value as null until such a point where a value needs to be added to the array, and then at that time create the array and then add the value to it. Kindly check this link

Importing JSON that starts with an array

I am trying to use excel to import and show a JSON. I've got this working for most JSONs. With this particular JSON, it starts with an array and I don't know how the parseOptions syntax to address and array first and then objects.I have successfully used =IMPORTJSON("url", "object to call") I want to display the '[0]/metadata/last_name' for each array object.
here is my code:
=IMPORTJSON("https://api.sleeper.app/v1/draft/547633764530618368/picks","[0]/metadata/last_name")
Here is my JSON:
[
{
"round":1,
"roster_id":null,
"player_id":"4866",
"picked_by":"448639868432543744",
"pick_no":1,
"metadata":{
"years_exp":"2",
"team":"NYG",
"status":"Active",
"sport":"nfl",
"position":"RB",
"player_id":"4866",
"number":"26",
"news_updated":"1577679319834",
"last_name":"Barkley",
"injury_status":"",
"first_name":"Saquon"
},
"is_keeper":null,
"draft_slot":1,
"draft_id":"547633764530618368"
},
{
"round":1,
"roster_id":null,
"player_id":"4046",
"picked_by":"341432375512702976",
"pick_no":2,
"metadata":{
"years_exp":"3",
"team":"KC",
"status":"Active",
"sport":"nfl",
"position":"QB",
"player_id":"4046",
"number":"15",
"news_updated":"1583891151374",
"last_name":"Mahomes",
"injury_status":"",
"first_name":"Patrick"
},
"is_keeper":null,
"draft_slot":2,
"draft_id":"547633764530618368"
},
{
"round":1,
"roster_id":null,
"player_id":"4881",
"picked_by":"539512871341760512",
"pick_no":3,
"metadata":{
"years_exp":"2",
"team":"BAL",
"status":"Active",
"sport":"nfl",
"position":"QB",
"player_id":"4881",
"number":"8",
"news_updated":"1580608524794",
"last_name":"Jackson",
"injury_status":"Probable",
"first_name":"Lamar"
},
"is_keeper":null,
"draft_slot":3,
"draft_id":"547633764530618368"
}
Okay, I figured the first part out. code looks like this:
\\
=IMPORTJSON("https://api.sleeper.app/v1/draft/547633764530618368/picks","0/metadata/first_name")
\\
But now I'm trying to have the cells run through a FOR Loop of the array. So it starts at 0 (as in 0/metadata/first_name), but how do I set it in a loop so that each cell increased the array number?

Getting values from json array using an array of object and keys in Python

I'm a Python newbie and I'm trying to write a script to extract json keys by passing the keys dinamically, reading them from a csv.
First of all this is my first post and I'm sorry if my questions are banals and if the code is incomplete but it's just a pseudo code to understand the problem (I hope not to complicate it...)
The following partial code retrieves the values from three key (group, user and id or username) but I'd like to load the objects and key from a csv to make them dinamicals.
Input json
{
"fullname": "The Full Name",
"group": {
"user": {
"id": 1,
"username": "John Doe"
},
"location": {
"x": "1234567",
"y": "9876543"
}
},
"color": {
"code": "ffffff",
"type" : "plastic"
}
}
Python code...
...
url = urlopen(jsonFile)
data = json.loads(url.read())
id = (data["group"]["user"]["id"])
username = (data["group"]["user"]["username"])
...
File.csv loaded into an array. Each line contains one or more keys.
fullname;
group,user,id;
group,user,username;
group,location,x;
group,location,y;
color,code;
The questions are: can I use a variable containing the object or key to be extract?
And how can I specify how many keys there are in the keys array to put them into the data([ ][ ]...) using only one line?
Something like this pseudo code:
...
url = urlopen(jsonFile)
data = json.loads(url.read())
...
keys = line.split(',')
...
# using keys[] to identify the objects and keys
value = (data[keys[0]][keys[1]][keys[2]])
...
But the line value = (data[keys[0]][keys[1]][keys[2]]) should have the exact number of the keys per line read from the csv.
Or I must to make some "if" lines like these?:
...
if len(keys) == 3:
value = (data[keys[0]][keys[1]][keys[2]])
if len(keys) == 2:
value = (data[keys[0]][keys[1]])
...
Many thanks!
I'm not sure I completely understand your question, but I would suggest you to try and play with pandas. It might be as easy as this:
import pandas as pd
df = pd.read_json(<yourJsonFile>, orient='columns')
name = df.fullname[0]
group_user = df.group.user
group_location = df.group.location
color_type = df.color.type
color_code = df.color.code
(Where group_user and group_location will be python dictionaries).

Construct unique arrays from nested array values with common parents

Likely a close question to JQ: Nested JSON transformation but I wasn't able to get my head around it.
Sample JSON:
"value": [
{
"FeatureStatus": [
{
"FeatureName": "Sway1",
"FeatureServiceStatus": "ServiceOperational"
},
{
"FeatureName": "Sway2",
"FeatureServiceStatus": "ServiceDegraded"
}
],
"Id": "SwayEnterprise",
},
{
"FeatureStatus": [
{
"FeatureName": "yammerfeatures",
"FeatureServiceStatus": "ServiceOperational"
}
],
"Id": "yammer"
}
]
What I want to do is create an output with jq which results in the following;
{"Sway":"Sway1":"ServiceOperational"},
{"Sway":"Sway2":"ServiceDegraded"},
{"Yammer":"yammerfeatures":"ServiceOperational"}
My various attempts either end up with thousands of non-unique (i.e Yammer with Sway status), or only one Id with x number of FeatureServiceStatus.
Any pointers would be greatly appreciated. I've gone through the tutorial and the cookbook. I am perhaps 2.5 days into using jq.
Assuming that the enclosing braces have been added to make the input valid JSON, the filter:
.value[]
| [.Id] + (.FeatureStatus[] | [ .FeatureName, .FeatureServiceStatus ])
produces:
["SwayEnterprise","Sway1","ServiceOperational"]
["SwayEnterprise","Sway2","ServiceDegraded"]
["yammer","yammerfeatures","ServiceOperational"]
You can then easily reformat this as desired.

Json creation in ruby for a list

I am new to Ruby.
I want to create a JSON file for a group of elements.
For this, I am using eachfunction to retrieve the datas. I want to create json as follows for the 4 length array,
'{
"desc":{
"1":"1st Value",
"2":"2nd value"
"3":"3rd Value",
"4":"4th value"
},
}'
This is my array iteration,
REXML::XPath.each( doc, "//time" ) { |element1|
puts element1.get_text
}
I know here is the simple code to generate a JSON,
require 'json/add/core'
class Item < Struct.new(:id, :name); end
chair = Item.new(1, 'chair')
puts JSON.pretty_generate(chair)
This syntax will generate a json as follows,
{
"json_class": "Item",
"v": [
1,
"chair"
]
}
But I'm not sure how to do that to make JSON for my elements as stated above. Google search didn't give me a proper way to do this.
Can anyone help me here?
it means this?:
require 'json'
my_arr= ["1st Value","2nd Value","3rd Value","4th Value"]
tmp_str= {}
tmp_str["desc"] = {}
my_arr.each do |x|
tmp_str["desc"]["#{x[0]}"] = x
end
puts JSON.generate(tmp_str)
you can iterate the string array ,then take the strings to hash object.JSON can easy to parse Hash objcect .

Resources