Convert Json Key Value Pair

[{
“FieldName” : “city”,
“FieldValue” : “NewYork”
},
{
“FieldName” : “Zip Code”,
“FieldValue” : “1234”
}
]

i wanted to convert above json to below format,can anyone please help

[{
“City” : “NewYork”,
“Zip Code” : “123”
}]

Hi @shanmugaraj27may,

First and foremost the sample you provided is not a Json String. A Json array cannot be the starting point, you need to assign it to a key.

{"JsonArray" : [
	{
	"FieldName" : "city",
	"FieldValue" : "NewYork"
	},
	{
	"FieldName" : "Zip Code",
	"FieldValue" : "1234"
	}
]
}

Now the request you have is quite straight forward only when the Index of each of the Json Strings with the JsonArray is constant. I assume it is.

You can use the index to extract the required values.
Key1 = JsonArray("JsonArray")(0)("FieldName").ToString
Value1= JsonArray("JsonArray")(0)("FieldValue").ToString

Similarly for Key 2
Key2 = JsonArray("JsonArray")(1)("FieldName").ToString
Value2= JsonArray("JsonArray")(1)("FieldValue").ToString

Your output text needs to be formated in Json string format using
OuputText = "{"+""""+Key1+""""+":"+""""+Value1+""""+","+""""+Key2+""""+":"+""""+Value2+""""+"}"

This OutputText can be parsed as a jobject using
Newtonsoft.Json.Linq.JObject.Parse(OutputText)
This is how it would look in the end. You can then use the keys to extract values. For example,
OutputJson("city")

Sample files : JsonExtraction.json (142 Bytes)
ExtractAndFormatJson.xaml (10.6 KB)

Hope this clarifies some of your doubts!

1 Like

Hi

here you go with an xaml on this
regex.zip (2.4 KB)

Cheers @shanmugaraj27may

1 Like

@shanmugaraj27may
Welcome to the forum

as it is JSON we would process it preferred with JSON Api instead of Text approaches

For this structure:

Can doit with a oneliner (for String)

 "[" & JSonConvert.SerializeObject(JArray.Parse(strJSON).ToDictionary(Function (x) x("FieldName").Value(Of String), Function (x) x("FieldValue").Value(Of String)))& "]"

for JArray
grafik

JArray.Parse("[" & JSonConvert.SerializeObject(JArray.Parse(strJSON).ToDictionary(Function (x) x("FieldName").Value(Of String), Function (x) x("FieldValue").Value(Of String)))& "]")

But lets shift away from hard to maintain oneliners and lets do following:
Vars:


Flow:
grafik
With Parameters:
grafik
grafik

Find starter help here:
Flat_JArrayObjectsToSingleJObjectArray.xaml (7.2 KB)
data.json (114 Bytes)

2 Likes

This Structure:

myJObject("JsonArray").Values(Of JObject).ToDictionary(Function (x) x("FieldName").Value(Of String), Function (x) x("FieldValue").Value(Of String))

The dict we can Deserialize into an JOBject and would look like:
grafik

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.