Parse a JsonVector - json

I'm not experienced with this topic, so i hope you guys might help me.
So i've written a Json file with the Json lib. Everything seems fine, the file has the structure i need to work with.
1.) I'm not sure of my json format is correct to be parseable.
EDIT---->Added new (correctly formatted) json file:
{
"Jobs": [
{
"ExitCode" : "259",
"Id" : "00000021",
},
{
"ExitCode" : "259",
"Id" : "00000022",
},
]
}
I think i miss something to identify the key/value pairs? Or could i still read those 2 fields seperately?
Used Code to generate:
PROT()<<"Writing File ----"<<endl;
StructType Struct;
Json::Value JsonVec(Json::arrayValue);
//TCHAR szBuf[MAX_PATH];
for(GP_ITERATOR(JobIt, JobMap))
{
JobIt->second.Job.APIBind(Struct);
Json::Value JsonJob;
Struct.SaveToJSON(JsonJob);
JsonVec.append(JsonJob);
}
JsonVec.append("Job:");
std::ofstream file_id;
file_id.open("test.txt");
Json::StyledWriter styledWriter;
file_id << styledWriter.write(JsonVec);
file_id.close();

Related

handling a well-formed JSON file of an array of objects

A JSON string string passes the jsonlint test.
response = [
{
"article" : {
"info" : {
"initial" : {
"articleIds" : [
"7461221587662919569"
],
}
},
"text" : "where they would 'transfer to' next.",
"lang" : "en",
}
},
{
"article" : {
"info" : {
"initial" : {
"articleIds" : [
"6613144915874808065"
],
}
},
"text" : "produto regional.",
"lang" : "pt"
}
}
]
However, after processing
require 'json'
file = File.read('/Users/main/jugg//article_samples.js')
data_hash = JSON.parse(file)
One is left with an array, whereas more frequently a hash with a name labels a subsequent array, where one works with that nomenclature such as response['data']
But in this case the array is not accessible via response[0]. How can this be considered as an array in order to process each individual element collection.each do |member|?
A curiosity: data_hash.class => NilClass
The response = ... code from article_samples.js is JavaScript, not JSON. This initializes a variable named response with a JavaScript array.
To use this as JSON, then rename the file to article_samples.json and remove response = from the file. The first line should start with [.
Now your second block of code should work just fine as long as the article_samples.json file is in the correct path.
On a side note, I suggest that you find a way to make the path more flexible. The way you have it currently hard coded is tied directly to your current machine's file system. This won't work if you want to run this code from another machine because the folder /Users/main/jugg probalby won't exist.
If this is a web server with ruby on rails, then one solution is to create an environment variable with the path where this file is stored.

How to access data from nested dicts json?

I want to get some data from a json file. I can access everything with the code below.
import json
with open('C:\\Users\\me\\Documents\\stdin.json', 'r', encoding='utf8', errors='ignore') as json_file:
data = json.load(json_file)
print("Type: ", type(data))
print("VM: ", data["Datacenter"])
The .json file looks like this:
{
"Datacenter":[
{
"Folder":[
{
"Folder":[
{
"VirtualMachine":[
{
"moid":"vm-239566",
"name":"DEV CentOS 6",
},
{
"moid":"vm-239464",
"name":"DEV Sles 12",
},
],
"moid":"group-v239127",
"name":"DEV-VMs"
},
],
"moid":"group-v78",
"name":"Test and Dev"
},
{
"VirtualMachine":[
{
"moid":"vm-66130",
"name":"Hyv16-clone",
}
],
"moid":"group-v77",
"name":"Templates"
}
],
"moid":"datacenter-21",
"name":"Datencenter"
}
],
"vSphereHost":"srv01",
"vSphereProductLine":"vpx",
"vSphereServer":"VMware vCenter Server",
"vSphereVersion":"xxx",
"version":"1.0",
"viewType":"VMs and Templates"
}
Note that the original json file was much bigger as I deleted lines for readabilty. Also note that I run everything from Command Line, as my IDE always gives me the error UnicodeEncodeError: 'charmap' codec can't encode characters in position 22910-22912: character maps to <undefined>
I tried to use data["VirtualMachine"] instead of data["Datacenter"] but then I get an error... TypeError: 'VirtualMachine' is an invalid keyword argument for this function.
So how can I get/print the moid and name of a VM? I am really new to coding and donĀ“t know how to deal with nested dictionarys
However your question does not seems clear but from whatever you have mentioned this can be help you deriving the value in nested json if you have already have dataframe created. You can go ahead and add get() till you reach what you require. Below is the sample that you can use
import json
data = data.apply(lambda x: json.loads(json.loads(x).get("Folder","{}")).get("moid") if x else None)

How to extract JSON path separately?

I'm new to the JSON and I'm wondering how to extract a path from this kind of URL- https://www.example.com/bio-chocolate-p952
separately this way: product - bio-chocolate; product_id: p952
All I was able to come up with is following JSON pattern, which extract the path together:
{
"v": 7,
"domain": "example.com",
"path.list": [
{
"$regex": "[a-z0-9-]+-[p0-9]{1,}",
"extract": "product"
}
]
}
Hopefully I made myself clear. Anyways thank you for your answers and time.
Have a nice day.
Michal
If you have pathString = 'bio-chocolate-p952' You can use pathString.split('-') which will return an array ['bio','chocolate','p952']

Transform XML into a Map using groovy

I have some XML that I want to transform into a Map. I used the middle way of transforming XML to JSON and then greating a map of the json:
import org.json.XML
import groovy.json.JsonSlurper
Map parseXml(String input) {
String json = XML.toJSONObject(input).toString()
new JsonSlurper().parseText(json)
}
But when you have name spacing, it does not get removed.
eg.
<ns2:someObject xmlns:ns2="http://someLink">
<someOtherObject>
<something>SOME_THING</something>
</someOtherObject>
<someOtherObject>
<something>SOME_THING_ELSE</something>
</someOtherObject>
</ns2:someObject>
will end up in
{
"ns2:someObject": {
"xmlns:ns2": "http://someLink",
"someOtherObject": [
{
"something": "SOME_THING"
},
{
"something": "SOME_THING_ELSE"
}
]
}
}
But I want it to end up like this:
{
"someObject": [
{
"something": "SOME_THING"
},
{
"something": "SOME_THING_ELSE"
}
]
}
Does anyone have an idea how I can achive that without reinventing the wheel?
I already found a post a bit similar to mine, but it has a different approach, that's why I asked my question.
The example I gave is just an example, but there dan be multiple entries of someObjects which the given answer in the other post does not include.
Secondly it does iterate over the XML and creates a map of it - that is what I meant with reinventing the wheel. I am sure there must be a library doing exactly that already, so for me it seems wrong to write the parsing code myself.
Many thanks

d3js forced directed cannot read from json

I have a simple json file which is :
{
"nodes":[
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
],
"links":[
{"source":35,"target":44,"value":1},
{"source":44,"target":35,"value":1},
{"source":45,"target":35,"value":1},
{"source":45,"target":44,"value":1},
{"source":35,"target":49,"value":1},
{"source":49,"target":35,"value":1}
]
}
when I save it use exactly the html code as shown in http://bl.ocks.org/4062045#index.html and address the above json, nothing appears on the cancas.
I appreciate it if you help me with this one as I am not very familiar with it. Moreover, it would be great if I know the minimum code required for drawing a graph like this using json.
Best,
The number of "source" and "target" refer to the index of the item in nodes array.
So you can change your json to following:
{
"nodes":[
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
],
"links":[
{"source":0,"target":1,"value":1},
{"source":1,"target":2,"value":1},
{"source":2,"target":3,"value":1},
{"source":3,"target":4,"value":1},
]
}
Then you can just copy the codes from http://bl.ocks.org/4062045#index.html example as the minimum code.
Remenber to change the json file to your own json file.
d3.json("path/to/your/json", function(error, graph) {
//codes
});