I have a dataframe in Julia like df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"]). I have to convert it into a JSON like
{
"nodes": [
{
"A": "1",
"B": "M"
},
{
"A": "2",
"B": "F"
},
{
"A": "3",
"B": "F"
},
{
"A": "4",
"B": "M"
}
]
}
Please help me in this.
There isn't a method in DataFrames to do this. In a github issue where the following snippet, using JSON.jl, is offered as a method to write json:
using JSON
using DataFrames
function df2json(df::DataFrame)
len = length(df[:,1])
indices = names(df)
jsonarray = [Dict([string(index) => (isna(df[index][i])? nothing : df[index][i])
for index in indices])
for i in 1:len]
return JSON.json(jsonarray)
end
function writejson(path::String,df::DataFrame)
open(path,"w") do f
write(f,df2json(df))
end
end
JSONTables package provides JSON conversion to/from Tables.jl-compatible sources like DataFrame.
using DataFrames
using JSONTables
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
jsonstr = objecttable(df)
Related
I want to read a JSON value using a dynamic key using Python. For example, I have a JSON in the following format
{
"Personal": {
"Address": {
"City": "Newyork",
"Country": "USA"
}
},
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
}
}
If I enter a string as "Personal.Address.City", I need to get city value using Python.
It seems like you have to split the key and look up level-by-level manually. I give two implementations below:
data = {
"Personal": {
"Address": {
"City": "Newyork",
"Country": "USA"
}
},
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
}
}
key = 'Personal.Address.City'
def lookup_dot_separated_key(data, key):
value = data
for k in key.split('.'):
value = value[k]
return value
print(lookup_dot_separated_key(data, key))
def lookup_key_list(data, keys):
if keys:
return lookup_key_list(data[keys[0]], keys[1:])
else:
return data
print(lookup_key_list(data, key.split('.')))
I want to convert Json Object to Json Array in Karate to use 'match each' func.
I am getting to ('match each' failed, not a json array) error when I use match each func with Json Object.
Here is My Json Object:
{
{ "a": "q"
"b": "w",
"c": "t"
},
{ "a": "x"
"b": "y",
"c": "z"
}
}
And here is what I need:
[
{
{ "a": "q"
"b": "w",
"c": "t"
},
{ "a": "x"
"b": "y",
"c": "z"
}
}
]
Try this approach, using embedded expressions: https://github.com/intuit/karate#embedded-expressions
* def foo = { a: 1 }
* def list = [ '#(foo)' ]
* match each list == foo
I need to keep the first level JSON keys and convert the values to escaped strings, but only in case the values are also JSON objects. How can this be done in Groovy?
Input sample:
{
"a": "1",
"b": {
"c": "2",
"d": {
"e": "3"
}
},
"f": "4"
}
Desired result:
{
"a": "1",
"b": "{ \"c\": \"2\", \"d\": { \"e\": \"3\"} }",
"f": "4"
}
If you use JsonSlurper to parse the input JSON, then any nested JSON object will be represented as a LazyMap. You can use this information to collect all entries from the parsed JSON object (which is also a map) and convert any map object to its JSON string representation. You can convert any value to a JSON string representation using groovy.json.JsonOutput.toJson(object) method.
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def input = '''{
"a": "1",
"b": {
"c": "2",
"d": {
"e": "3"
}
},
"f": "4"
}'''
def json = new JsonSlurper().parseText(input)
def escaped = json.collectEntries { k,v ->
[(k): v instanceof Map ? new JsonOutput().toJson(v) : v]
}
def output = new JsonOutput().prettyPrint(JsonOutput.toJson(escaped))
println output
Output:
{
"a": "1",
"b": "{\"c\":\"2\",\"d\":{\"e\":\"3\"}}",
"f": "4"
}
I was trying to match particular variable from response and tried as below. But im getting error saying KarateException Missing Property in path $['Odata']. My question is: how we can modify so that we won't get this error?
Feature:
And match response.#odata.context.a.b contains '<b>'
Examples:
|b|
|b1 |
|b2 |
Response is
{
"#odata.context": "$metadata#Accounts",
"a": [
{
"c": 145729,
"b": "b1",
"d": "ON",
},
{
"c": 145729,
"b": "b2",
"d": "ON",
}
]
}
I think you are confused with the structure of your JSON. Also note that when the JSON key has special characters, you need to change the way you use them in path expressions. You can try paste the below in a new Scenario and see it work:
* def response =
"""
{
"#odata.context": "$metadata#Accounts",
"a": [
{
"c": 145729,
"b": "b1",
"d": "ON",
},
{
"c": 145729,
"b": "b2",
"d": "ON",
}
]
}
"""
* match response['#odata.context'] == '$metadata#Accounts'
* match response.a[0].b == 'b1'
* match response.a[1].b == 'b2'
I want to generate JSON from TOML files. The JSON structure should be something like this, with arrays of objects within arrays of objects:
{
"things": [
{
"a": "thing1",
"b": "fdsa",
"multiline": "Some sample text."
},
{
"a": "Something else",
"b": "zxcv",
"multiline": "Multiline string",
"objs": [ // LOOK HERE
{ "x": 1},
{ "x": 4 },
{ "x": 3 }
]
},
{
"a": "3",
"b": "asdf",
"multiline": "thing 3.\nanother line"
}
]
}
I have some TOML that looks like the example below, but it doesn't seem to work with the objs section.
name = "A Test of the TOML Parser"
[[things]]
a = "thing1"
b = "fdsa"
multiLine = """
Some sample text."""
[[things]]
a = "Something else"
b = "zxcv"
multiLine = """
Multiline string"""
[[things.objs]] # MY QUESTION IS ABOUT THIS PART
x = 1
[[things.objs]]
x = 4
[[things.objs]]
x = 3
[[things]]
a = "3"
b = "asdf"
multiLine = """
thing 3.
another line"""
Is there a way to do it in TOML? JSON to TOML converters don't seem to work with my example. And does it work with deeper nesting of arrays of arrays/tables?
As per the PR that merged this feature in the main TOML repository, this is the correct syntax for arrays of objects:
[[products]]
name = "Hammer"
sku = 738594937
[[products]]
[[products]]
name = "Nail"
sku = 284758393
color = "gray"
Which would produce the following equivalent JSON:
{
"products": [
{ "name": "Hammer", "sku": 738594937 },
{ },
{ "name": "Nail", "sku": 284758393, "color": "gray" }
]
}
I'm not sure why it wasn't working before, but this seems to work:
name = "A Test of the TOML Parser"
[[things]]
a = "thing1"
b = "fdsa"
multiLine = """
Some sample text."""
[[things]]
a = "Something else"
b = "zxcv"
multiLine = """
Multiline string"""
[[things.objs]]
x = 1
[[things.objs]]
x = 4
[[things.objs]]
x = 7
[[things.objs.morethings]]
y = [
2,
3,
4
]
[[things.objs.morethings]]
y = 9
[[things]]
a = "3"
b = "asdf"
multiLine = """
thing 3.
another line"""
JSON output:
{
"name": "A Test of the TOML Parser",
"things": [{
"a": "thing1",
"b": "fdsa",
"multiLine": "Some sample text."
}, {
"a": "Something else",
"b": "zxcv",
"multiLine": "Multiline string",
"objs": [{
"x": 1
}, {
"x": 4
}, {
"x": 7,
"morethings": [{
"y": [2, 3, 4]
}, {
"y": 9
}]
}]
}, {
"a": "3",
"b": "asdf",
"multiLine": "thing 3.\\nanother line"
}]
}